我一直在拉我的头发试图弄清楚这一点,但没有什么工作。我有一个网页在 mysite.com/test.php 我想做一个简单的URL重写,并将其更改为 mysite.com/testRewrite / 的code到不实现这个应该是:
I have been pulling my hair trying to figure this out but nothing is working. I have a webpage at mysite.com/test.php I want to do a simple URL rewrite and change it to mysite.com/testRewrite/ The code to do implement this should be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
无论出于何种原因,它只是不工作。当你去到新的URL。 - mysite.com/testRewrite / 它的工作原理
不过,如果你在原来的网址键入 mysite.com/test.php 它不会将地址更改为在浏览器新的URL。
However, if you type in the original URL mysite.com/test.php it doesn't change the address to the new URL in the browser.
推荐答案
mod_rewrite的将不会自动实施从重写的URL重定向浏览器。你只需把规则说:如果有人问 / testRewrite /
,在内部将其更改为 /test.php
。它什么都不做来处理 /test.php
实际的要求,这就是为什么当您试图访问的 mysite.com/test.php 时,为您提供了 /test.php
。
mod_rewrite won't automatically enforce redirecting browsers from rewritten URLs. The rule you have simply says "if someone asks for /testRewrite/
, internally change it to /test.php
". It does nothing to handle actual requests for /test.php
, which is why when you try to access mysite.com/test.php, it gives you /test.php
.
现在,与mod_rewrite的问题是,如果你只添加的规则:
Now, the problem with mod_rewrite is if you simply add that rule:
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
这将重定向浏览器时,它要求 /test.php
到 / testRewrite /
,第一条规则重定向浏览器之后将被应用,并且URI被改写为 /test.php
,然后它的需要对整个重写引擎的和上述规则被再次应用,从而将浏览器重定向,因此第一条规则重写到 /test.php
,和上面的规则再次重定向,等等,等等你得到一个重定向循环。你必须添加一个条件,以确保实际请求 /test.php
浏览器*,而且它不是多数民众赞成通过重写引擎被搅动的URI:
which will redirect the browser when it asks for /test.php
to /testRewrite/
, the first rule will be applied after the browser redirects, and the URI gets rewritten to /test.php
, then it goes back through the entire rewrite engine and the above rule gets applied again, thus redirecting the browser, thus the first rule rewrites to /test.php
, and the above rule redirects again, etc. etc. You get a redirect loop. You have to add a condition to ensure the browser *actually requested /test.php
and that it's not a URI that's been churned through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /test.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
这样一来,301重定向发生后,这条规则将不会被再次应用,因为实际的请求将 GET / testRewrite / HTTP / 1.1
,条件将无法实现。
This way, after the 301 redirect happens, this rule won't get applied again because the actual request will be GET /testRewrite/ HTTP/1.1
, and the condition won't be met.
相关推荐
最新文章