开发者

htaccess RewriteRule seems to be correct but not working

开发者 https://www.devze.com 2023-03-02 20:53 出处:网络
I want to redirect all traffic going to any .php file to index.php. For example: domain.com/test.php domain.com/test/test.php

I want to redirect all traffic going to any .php file to index.php.

For example:

domain.com/test.php

domain.com/test/test.php

.. should go to domain.com/index.php. Similarly:

www.domain.com/test.php

www.domain.com/test/test.php

.. should go to www.domain.com/index.php.

I have added the following rule:

RewriteRule (..com)(/.)?/(.*.php) $1/index.php

which according to an online regex tester should give me the right result, but when I use it on the actual htaccess the rule seems to be ignored, and a 404 error is given开发者_如何学JAVA instead.

What am I doing wrong?


The RewriteRule does not match against the host in its first parameter. Try the following:

RewriteEngine on

RewriteCond %{REQUEST_URI} index.php$
RewriteCond %{QUERY_STRING} arg=something
RewriteRule \.php$ /index.php? [R]

RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule \.php$ /index.php? [R]

Hope that helps.


Try this code:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# internally redirect all .php files to index.php
RewriteCond %{REQUEST_URI} !^/+index\.php [NC]
RewriteRule \.php$ /index.php? [L,NC]

# remove all query string from /index.php
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^index\.php$ /index.php? [L,R]
0

精彩评论

暂无评论...
验证码 换一张
取 消