mod_rewrite的不匹配的RewriteCond目标 - 找不到网址 [英] mod_rewrite don't match on RewriteCond target - URL not found

查看:190
本文介绍了mod_rewrite的不匹配的RewriteCond目标 - 找不到网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写目录/文件中的所有URI来/docs/index.php,使用阿帕奇mod_rewrite的模块。

的URI不存在作为一个目录结构,我不认为这是必要的,当你重写URI的东西,退出。

我收到错误:


  

所请求的URL /文档/视图/ my_doc在此服务器上找到。


这是我的重写规则:

  RewriteEngine叙述在
   的RewriteCond%{REQUEST_URI} ^文档/([A-Z] +)/([A-ZA-Z0-9 _-] +)/ $?
   (。*)重写规则^ $的http://%{HTTP_HOST} /docs/index.php?action=$1&target=$2 [QSA,L]

我做在httpd.conf配置。另外,我使用的是多V型主机设置。

什么是我的问题,如何解决?

我使用Apache / httpd的在CentOS 6.2


更新

包括我的虚拟主机,以及:

 了NameVirtualHost *:80<虚拟主机*:80>
    服务器名www.smicloud.org
    ServerAlias​​ * .smicloud.org
    VirtualDocumentRoot /var/www/html/www.smicloud.org
    ExpiresActive On配置
    ExpiresDefault访问加14日
    #插入记录的配置,你还需要什么..
    #定义FileEtag所以它不使用节点
    FileETag的mtime尺寸
    <目录/ var / www / html等/>
        为了允许,拒绝
        所有允许
        #摆脱这个,如果你需要允许htaccess的文件:
        设置AllowOverride无
    < /目录>
        RewriteEngine叙述在
        重写规则^文档/([A-Z] +)/([A-ZA-Z0-9 _-] +)/ $ /docs/index.php?action=$1&target=$2 [QSA,L]
< /虚拟主机>


解决方案

好吧,有几件事情,我不得不为了修复它做的,用@donalds建议(这实际上是我尝试过):

1 - 我必须有一个?在重写规则的正则表达式的结尾:


  

^ /文档/([A-Z] +)/([A-ZA-Z0-9 _-] +)/ $?


然后我可以选择是否有该地址的/或没有底。

2 - 我不得不添加一个/到我的重写规则的正则表达式的开始。即:


  

^ /文档/([A-Z] +)/([A-ZA-Z0-9 _-] +)$


3 - 我也不得不添加的http://%{HTTP_HOST} /'部分新的URI:


  

的http:// %{HTTP_HOST} /docs/index.php?action=$1&target=$2 [QSA,L]


这是我结束了:


  

重写规则^ /文档/([A-Z] +)/([A-ZA-Z0-9 _-] +)$
  的http:// %{HTTP_HOST} /docs/index.php?action=$1&target=$2 [QSA,L]


也许就是为什么我需要添加%{HTTP:HOST}是因为它是一个多虚拟主机设置。

I am trying to rewrite all URIs inside directory /docs to /docs/index.php, using apaches mod_rewrite module.

The URI doesn't exist as a directory structure, which I don't think is necessary when you are rewriting the URI to something that exit.

I am getting the error:

"The requested URL /docs/view/my_doc was not found on this server."

These are my rewrite rules:

   RewriteEngine On
   RewriteCond %{REQUEST_URI} ^docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
   RewriteRule ^(.*)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]

I am doing the configuration in httpd.conf. Also, I am using a multi v-host setup.

What's my problem and how do I fix it?

I'm using Apache/httpd on CentOS 6.2


UPDATE

Including my VirtualHost as well:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.smicloud.org
    ServerAlias *.smicloud.org
    VirtualDocumentRoot /var/www/html/www.smicloud.org
    ExpiresActive On
    ExpiresDefault "access plus 14 days"
    # insert logging config, anything else you need..
    #Define FileEtag so it doesnt use node
    FileETag MTime Size
    <Directory /var/www/html/>
        Order Allow,Deny
        Allow from all
        # Get rid of this if you need to allow htaccess files:
        AllowOverride None
    </Directory>
        RewriteEngine On        
        RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
</VirtualHost>

解决方案

Ok, there were a couple of things that I had to do in order to fix it, using @donalds suggestion (which I actually tried before):

1 - I had to have a '?' at the end of the regex of RewriteRule:

^/docs/([a-z]+)/([A-Za-z0-9_-]+)/?$

Then I can chose whether to have a '/' or not at the end of the address.

2 - I had to add a '/' to the beginning of the regex of my RewriteRule. I.e.:

^/docs/([a-z]+)/([A-Za-z0-9_-]+)$

3 - I also had to add the 'http://%{HTTP_HOST}/' part to the new URI:

http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]

This is what I ended up with:

RewriteRule ^/docs/([a-z]+)/([A-Za-z0-9_-]+)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]

Perhaps why I needed to add the %{HTTP:HOST} was because it's a multi virtual host setup.

这篇关于mod_rewrite的不匹配的RewriteCond目标 - 找不到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆