Question

I have a rewrite rule in my .htaccess.

RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]

Which redirects mysite.com/mypage to mysite.com/user_home.php?domain=mypage

This works fine on my shared host, but when working locally, my site is located in www/mysite/ and instead of redirecting to www/mysite/user_home.php?domain=mypage it tries to redirect to www/user_home.php?domain=mypage which doesn't exist.

I get the following error in my Apache Logs

[error] [client ::1] script 'C:/wamp/www/user_home.php' not found or unable to stat

How can I alter my .htaccess to make it direct to the file in the current directory using a relative path instead of an absolute path..

Was it helpful?

Solution

You just need to remove leading slash from the target URL in your rule like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ user_home.php?domain=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f is also needed to prevent infinite looping.

OTHER TIPS

Try this code :

RewriteBase /mysite/
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]

You just have to add a RewriteBase with the path of the root folder of your website.

The best solution would be to add checks if there is a file called like this and set a RewriteBase:

RewriteEngine On
RewriteBase mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [QSA,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top