remove index.php url rewriting .htaccess

别等时光非礼了梦想. 提交于 2020-02-01 04:36:15

问题


I'd written this code for 301 redirect

RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

It is working well in case if I do visit my site as http://mysite.com/index.php, it redirects me to http://mysite.com

But on my localhost if I try to visit index.php as localhost/mysite/index.php it redirects me to localhost.

How could I solve this problem? Is the code written above is correct?


回答1:


It looks like you have your htaccess file in your document root on your server, and in the mysite directory on localhost. Since the location of the htaccess file is pretty important on how it routes URIs, you need to make it indifferent to the location of the file. You can do this by extracting the path info from your condition instead of the URI that's passed into the rule to match against:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

The %{THE_REQUEST} variable is the first line of the actual HTTP request, which looks something like:

GET /path/index.php HTTP/1.1

The pattern first matches any number of possible METHODS (GET, POST, HEAD, etc), then it creates a grouping of the URI path that's before the index.php, then ends the matching, since we don't really care what's after the index.php.




回答2:


try this

RewriteRule ^(.*)index\.php$ /$1 [R=301,NC] 



回答3:


If your site is not at the root of the server (which it's not on your localhost), you will need to add a RewriteBase directive: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

You need to add the line:

RewriteBase /mysite/

above the current lines in the htaccess on your localhost




回答4:


While RewriteBase directive stands for this usage, you may want to test for localhost and rewrite root to your subdirectory only if test == true. eg:

RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]

Note I have not used this for some time and cannot guarantee you it'll work out of the box; might need some edit.



来源:https://stackoverflow.com/questions/18742446/remove-index-php-url-rewriting-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!