Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache Mod Rewrite for Pretty URL不是';行不通_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache Mod Rewrite for Pretty URL不是';行不通

Apache Mod Rewrite for Pretty URL不是';行不通,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我正在试图找出如何进行apachemod\u rewrite以重新映射$\u GET 我正在努力实现的目标: 目前,要进入页面,必须转到 http://www.domain.com/index.php?URL=pages/the-page.php 我希望这可以通过两种方式实现: 如果有人进入domain.com/页面,它会把他们带到上面,但会保持这样的外观。其次,如果有人去http://www.domain.com/index.php?URL=pages/the-php,它仍将显示为domain

我正在试图找出如何进行apache
mod\u rewrite
以重新映射
$\u GET

我正在努力实现的目标:

目前,要进入页面,必须转到

http://www.domain.com/index.php?URL=pages/the-page.php

我希望这可以通过两种方式实现:

如果有人进入
domain.com/页面
,它会把他们带到上面,但会保持这样的外观。其次,如果有人去
http://www.domain.com/index.php?URL=pages/the-php
,它仍将显示为
domain.com/页面
,保持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并在live server上进行尝试

日志文件:


127.0.0.1---[5/Apr/2013:16:50:43--0400][localhost/sid#2f3140][rid#3b14068/initial](3)[perdir C:/xampp/htdocs/cdi/]strip per dir前缀: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/]将模式“^(.*)”应用于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前缀: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/]将模式“^”应用于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前缀: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/]将模式“^index\.php$”应用于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/]重写'index.php'->'/newhome?

127.0.0.1---[05/Apr/2013:20:02:24--0400][localhost/sid#2e3140][rid#3b14090/initial](3)分割uri=/newhome?->uri=/newhome,args=

127.0.0.1---[05/Apr/2013:20:02:24--0400][localhost/sid#2e3140][rid#3b14090/initial](2)[perdir C:/xampp/htdocs/cdi/]使用http://localhost/newhome
您可以尝试以下方法:

RewriteRule ^/([a-z0-9_-]{1,40})/?$ index.php?URL=pages/$1.php
RewriteCond %{REQUEST_FILENAME} !-f  

虽然理想情况下,您可能希望删除查询字符串变量的“pages/”部分,因为这个固定常量可以由index.php脚本处理。

如果您希望组
([0-9]+)
为字母,则只需将其更改为
([a-z]+)
,如果您希望它为字母数字,则将其更改为
([a-z0-9]+)
,以及
([a-z0-9-!]+)
,如果带有连字符和下划线。如果您希望它手动设置它们的限制,可以使用此格式
([a-z0-9-{1,40})
。你看,加号不见了,因为它将
[chars]
限制为1,而
{1,40}
[chars]
限制为1到40,你可以改变它


你知道真正的问题是什么吗?这是我的压力。。想象一下,即使我知道你想将/$var重新映射到/index.php?URL=pages/$var.php,我仍然试图给你一个错误的信息,将/index.php?URL=pages/$var.php重写到/$var。我刚刚意识到,在我睡了4个小时之后。你有没有看到当你的睡眠时间不对的时候发生了什么?也许当我的大脑功能正常时,我会给你一个规则,那就是:

RewriteRule ^([a-z0-9-_]+)/?$ /index.php?URL=pages/$1.php

为什么观众让这件事发生。。我以前的代码需要被否决。

您的方法似乎很好,但是您的
RewriteCond
不符合您的要求:

RewriteCond %{REQUEST_URI} ^index.php?URL=pages
意思是“如果有人请求以'index.php'开头的内容,请重写URL”-但这不是任何人都会请求的内容。您希望您的访问者请求漂亮的URL

如果您的服务器只需要为/页面的这些请求提供服务,则可以完全删除该条件。然后,任何URL都将被重写。(注意:这可能不是您想要的!)

否则,条件应如下所示:

RewriteCond %{REQUEST_URI} ^[a-z0-9-_]{1,40}
如果不想弄乱正则表达式,也可以尝试以下方法:

RewriteRule ^/([a-z0-9_-]{1,40})/?$ index.php?URL=pages/$1.php
RewriteCond %{REQUEST_FILENAME} !-f  

这意味着“如果用户请求一个找不到文件的URL,请根据即将发布的
重写规则重写URL”

基本上,您需要两条规则。一个规则是将客户端重定向到干净的URL,另一个规则是通过
index.php
在内部将漂亮的URL重写为真实内容

假设
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路径。然后,客户机请求新的URL
/cdi/abc
,第三条规则接受这一点,并对真实内容进行内部重写

这一切都可以正常工作,但会无限期地重写和重定向。要打破这个无休止的规则,第一个规则检查环境
%{