阿帕奇mod-rewrite为pretty的网址无法正常工作 [英] Apache Mod Rewrite for Pretty URLs isn't working
问题描述
我试图找出如何做一个apache 的mod_rewrite
来重新映射 $ _ GET
。
I'm trying to figure out how to do an apache mod_rewrite
to remap $_GET
.
我试图做到:
目前,去的第一页就不得不去
Currently, to get to the page one would have to go to
http://www.domain.com/index.php?URL=pages/the-page.php
我想这2种方式工作:
如果有人去 domain.com/the-page
,他们花上面,但保持它看起来像这样。其次,如果有人进入到 http://www.domain.com/index.php?URL=pages/the-page.php
,它仍将显示为 domain.com/the-page
,保持URL短而干净。
If someone goes to domain.com/the-page
, it takes them to the above but keeps it looking like this. Secondly, if someone goes to the http://www.domain.com/index.php?URL=pages/the-page.php
, it will still show as domain.com/the-page
, keeping the URL short and clean.
最近试了code
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} URL=pages/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /%1
我是pretty的肯定,我设置的一切权利在Apache 的httpd.conf
。我使用的XAMPP到本地测试,重启apache的上的变化,仍然一无所获。我在哪里去了?
I'm pretty sure I setup everything right in the apache httpd.conf
. I'm using XAMPP to test locally, restarted apache on changes, still nothing. Where am I going wrong?
我想preFER来处理这个在.htaccess 的
I would prefer to handle this in .htaccess
我使用的XAMPP本地主机,并试图活的服务器上。
I am using XAMPP localhost and trying on live server.
日志文件:
127.0.0.1 - - [05 / APR / 2013:16:50:43 --0400] [本地主机/ SID#2f3140] [摆脱#3b14068 /初始](3)[perdir C:/ XAMPP / htdocs中/ CDI /]带每DIR preFIX:C:/xampp/htdocs/cdi/index.php - >的index.php
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05 / APR / 2013:16:50:43 --0400] [本地主机/ SID#2f3140] [摆脱#3b14068 /初始](3)[perdirÇ :/ XAMPP / htdocs中/ CDI /]应用模式(。*)^'到URI的index.php
127.0.0.1 - - [05 /四月/ 2013:16:50:43 --0400] [本地主机/ SID#2f3140] [摆脱#3b14068 /初始](1)[perdirÇ :/ XAMPP / htdocs中/ CDI /]通过C:/xampp/htdocs/cdi/index.php
更新日志,Olaf的脚本的(最后一个规则注释掉)的
Updated log with Olaf's script (last rule commented out)
127.0.0.1 - - [05 / APR / 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](3)[perdirÇ :/ XAMPP / htdocs中/ CDI /]条每个DIR preFIX:C:/xampp/htdocs/cdi/index.php - >的index.php
127.0.0.1 - - [05 / APR / 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](3)[perdirÇ :/ XAMPP / htdocs中/ CDI /]应用模式'^'到URI的index.php
127.0.0.1 - - [05 /四月/ 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](3)[perdirÇ :/ XAMPP / htdocs中/ CDI /]条每个DIR preFIX:C:/xampp/htdocs/cdi/index.php - >的index.php
127.0.0.1 - - [05 /四月/ 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](3)[perdirÇ :/ XAMPP / htdocs中/ CDI /]应用模式'^指数\的.php $'到URI的index.php
127.0.0.1 - - [05 / APR / 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](2)[perdirÇ :/ XAMPP / htdocs中/ CDI /]改写'的index.php' - > / newhome上?
127.0.0.1 - - [05 /四月/ 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](3)分割URI = /新家? - > URI = / newhome上,ARGS =<没有>
127.0.0.1 - - [05 /四月/ 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](2)[perdirÇ :/ XAMPP / htdocs中/ CDI /]明确强制重定向以http://本地主机/ newhome上
的< - 这一次似乎是导致该问题的的
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] explicitly forcing redirect with http://localhost/newhome
<--this one seems to be causing the issue
127.0.0.1 - - [05 / APR / 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](1)[perdirÇ :/ XAMPP / htdocs中/ CDI /]逃逸的http://本地主机/ newhome上的重定向
127.0.0.1 - - [05 /四月/ 2013:20:02:24 --0400] [本地主机/ SID#2e3140] [摆脱#3b14090 /初始](1)[perdirÇ :/ XAMPP / htdocs中/ CDI /]重定向到http://本地主机/ newhome上[重定向/ 302]
谢谢大家的帮助。我花了2天尝试得到这个工作!
Thank you everyone that is helping. I've spent 2 days trying to get this to work!!!
推荐答案
基本上,你需要两个规则。一个规则将客户端重定向到一个干净的网址,并通过另一个内部重写pretty的URL的实际内容的index.php
。
Basically, you need two rules. One rule to redirect the client to a clean URL and another to internally rewrite the pretty URL to the real content via index.php
.
假设的index.php
和的.htaccess
是一个目录 CDI
RewriteEngine on
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?)\.php
RewriteRule ^index\.php$ /cdi/%1? [R,L]
# exclude rewriting all files located in /cdi/files
RewriteCond %{REQUEST_URI} !^/cdi/files/
# rewrite to real content
RewriteRule ^.*$ /cdi/index.php?URL=pages/$0.php [L]
更新
在请求 /cdi/index.php?URL=pages/abc.php
,第二条规则中提取所需要的网址
部分并重定向客户到新的URL路径。然后,客户端请求新的URL / CDI / ABC
和第三条规则从这个并执行内部重写的真实内容。
When the request is /cdi/index.php?URL=pages/abc.php
, the second rule extracts the needed URL
part and redirects the client to the new URL path. The client then requests the new URL /cdi/abc
and the third rule takes this and does an internal rewrite to the real content.
这一切工作正常,因为它应该,但会重写和重定向下去。为了打破这无尽的规则,第一个规则检查环境%{ENV:...}
,如果该请求已经被重定向 REDIRECT_STATUS
,然后停止循环与 重写规则
This all works fine as it should, but would rewrite and redirect indefinitely. To break this endless rule, the first rule checks the environment %{ENV:...}
, if the request was already redirected REDIRECT_STATUS
and then stops the cycle with the RewriteRule
RewriteRule ^ - [L]
匹配一切 ^
并不做任何的替换,但最终改写周期的标志的 [L]
which matches everything ^
and does no substitution, but ends the rewrite cycle with the flag [L]
而不是使用所提供的系统环境状态
/ REDIRECT_STATUS
,也可以设置一个变量自己的标志 E = SEO:1
例如,然后测试该变量与
Instead of using the system provided environment STATUS
/REDIRECT_STATUS
, you can also set a variable yourself with the flag E=SEO:1
for example, and then test for this variable with
RewriteCond %{ENV:REDIRECT_SEO} 1
对于重定向_
preFIX,看到的可用变量的。
For the REDIRECT_
prefix, see Available Variables.
这篇关于阿帕奇mod-rewrite为pretty的网址无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!