문제

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..

도움이 되었습니까?

해결책

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.

다른 팁

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top