【发布时间】:2013-03-23 19:01:02
【问题描述】:
我正在尝试弄清楚如何使用 apache mod_rewrite 重新映射 $_GET。
我想要完成的工作:
目前,要进入页面,必须去
http://www.domain.com/index.php?URL=pages/the-page.php
我希望它以两种方式工作:
如果有人去domain.com/the-page,它会将他们带到上面但保持它看起来像这样。其次,如果有人访问http://www.domain.com/index.php?URL=pages/the-page.php,它仍然会显示为domain.com/the-page,从而保持 URL 简洁明了。
最近尝试过的代码
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} URL=pages/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /%1
我很确定我在 apache httpd.conf 中正确设置了所有内容。我正在使用 XAMPP 在本地进行测试,在更改时重新启动 apache,仍然没有。我哪里错了?
我更愿意在 .htaccess 中处理这个问题
我正在使用 XAMPP localhost 并尝试在实时服务器上。
日志文件:
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] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^(.*)' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (1) [perdir C:/xampp/htdocs/cdi/] pass through C:/xampp/htdocs/cdi/index.php
使用 Olaf 的脚本更新日志(最后一条规则被注释掉)
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/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:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/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:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^index\.php$' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] rewrite 'index.php' -> '/newhome?'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) split uri=/newhome? -> uri=/newhome, args=<none>
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
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] escaping http://localhost/newhome for redirect
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] redirect to http://localhost/newhome [REDIRECT/302]
感谢所有提供帮助的人。我花了 2 天的时间试图让它工作!!!
【问题讨论】:
-
这仍然是您当前完整的 .htaccess 吗?
-
没有,但是我已经注释掉了我添加的 httpd.conf 代码,并按照说明删除了除了下面的代码之外的所有 .htaccess。
-
您到底想达到什么目标?如果只是将来自
/abc的传入请求重写为/index.php?URL=pages/abc.php,则删除RewriteCond。单独的RewriteRule应该可以正常工作。 -
更新了原始问题。
-
我刚刚回答了一个类似的问题,见stackoverflow.com/a/15839460/1741542
标签: apache .htaccess mod-rewrite