重写URL来回使用mod_rewrite [英] Rewriting urls back and forth using mod_rewrite
问题描述
也许会显得很简单的问题,我的网站的网址是要命写的,所以我要重写他们。所以我preparing激活mod_rewrite的。现在的问题是:
Maybe it will seem a very simple question, my website's url are awfully written, so I want to rewrite them. So I am preparing to activate mod_rewrite. The question is the following:
假设从 SITE_URL / index.php页面=我的空间和放我的网址开关; TYPE = MYTYPE
到 SITE_URL /我的页面/ MYTYPE
,如果刷新页面会发生什么事,是否有必要在.htaccess文件中提出的逆过程对投诉进行重写?
Suppose my url switches from site_url/index.php?page=mypage&type=mytype
to site_url/mypage/mytype
, what will happen if one refresh the page, is it necessary to put in the .htaccess file the converse process for proceeding to rewriting ?
推荐答案
您需要重定向和重写的组合。 URL重写过程的纲要草案将是:
You need a combination of redirect and rewriting. A draft outline of URL rewriting process would be:
- 如果URL中包含
的.php
301/302请求重定向到它的搜索引擎友好的版本。这将改变地址在浏览器地址栏。 - 如果被请求的搜索引擎友好的URL,把它改写到PHP版本。这不会更改的地址在浏览器地址栏。
- If URL contains
.php
, 301/302 redirect the request to its SEO friendly version. This will change the address in browser address bar. - If SEO friendly URL was requested, rewrite it to its PHP version. This will not change the address in browser address bar.
下面是一个非常基本的的例子是一举两得上面的重写规则:
Here is a very basic example of rewrite rules that do both of the above:
#
# redirect /index.php?page=somepage&type=sometype to friendly url
#
RewriteCond %{THE_REQUEST} /index\.php
RewriteCond %{QUERY_STRING} page=(\w+)&type=(\w+)
RewriteRule ^index\.php$ /%1/%2? [R,L]
#
# rewrite /somepage/sometype to index.php
#
RewriteRule ^(\w+)/(\w+)$ index.php?page=$1&type=$2
这篇关于重写URL来回使用mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!