删除index.php url重写.htaccess

时间:2022-08-29 11:01:13

I'd written this code for 301 redirect

我为301重定向编写了这段代码

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

如果我访问我的网站http://mysite.com/index.php,它运行良好,它重定向到http://mysite.com

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

但是在我的localhost上,如果我尝试访问index.php作为localhost / mysite / index.php,它会将我重定向到localhost。

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

我怎么能解决这个问题?上面写的代码是否正确?

4 个解决方案

#1


6  

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:

看起来您的服务器上的文档根目录中以及localhost上的mysite目录中有htaccess文件。由于htaccess文件的位置对于路由URI的方式非常重要,因此您需要使其对文件的位置无动于衷。您可以通过从条件中提取路径信息而不是传递到规则中以匹配的URI来执行此操作:

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:

%{THE_REQUEST}变量是实际HTTP请求的第一行,类似于:

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.

模式首先匹配任意数量的可能方法(GET,POST,HEAD等),然后它创建一个URI路径的分组,它在index.php之前,然后结束匹配,因为我们真的不关心什么是后面的index.php文件。

#2


4  

try this

尝试这个

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

#3


2  

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

如果您的站点不在服务器的根目录(它不在您的本地主机上),则需要添加RewriteBase指令: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

在localhost上的htaccess中的当前行之上

#4


0  

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:

虽然RewriteBase指令代表这种用法,但您可能希望测试localhost并仅在test == true时将root重写为您的子目录。例如:

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.

注意我已经有一段时间没有使用它了,不能保证它能开箱即用;可能需要一些编辑。

#1


6  

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:

看起来您的服务器上的文档根目录中以及localhost上的mysite目录中有htaccess文件。由于htaccess文件的位置对于路由URI的方式非常重要,因此您需要使其对文件的位置无动于衷。您可以通过从条件中提取路径信息而不是传递到规则中以匹配的URI来执行此操作:

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:

%{THE_REQUEST}变量是实际HTTP请求的第一行,类似于:

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.

模式首先匹配任意数量的可能方法(GET,POST,HEAD等),然后它创建一个URI路径的分组,它在index.php之前,然后结束匹配,因为我们真的不关心什么是后面的index.php文件。

#2


4  

try this

尝试这个

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

#3


2  

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

如果您的站点不在服务器的根目录(它不在您的本地主机上),则需要添加RewriteBase指令: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

在localhost上的htaccess中的当前行之上

#4


0  

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:

虽然RewriteBase指令代表这种用法,但您可能希望测试localhost并仅在test == true时将root重写为您的子目录。例如:

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.

注意我已经有一段时间没有使用它了,不能保证它能开箱即用;可能需要一些编辑。