重写规则的htaccess影响其他重写规则 [英] Rewrite rule htaccess disturbing other rewrite rules

查看:144
本文介绍了重写规则的htaccess影响其他重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站的一个网页,这是获得动态生成列出根据 cityf 参数的所有网点及以下重写规则将其转换成搜索引擎友好的URL,并将其正在pretty的好。

 重写规则^([^ /] +)/?$ / cityres?cityf = $ 1 [L]
 

我有一个博客网页上我的网站和.htaccess是如下转换搜索引擎友好的URL(http://example.com/title-of-blog <$ C C $>)

 重写规则^([^ /] +)/?$ / blogdetail?prmn = $ 1 [L]
 

现在问题就在这里,我面临着当有人访问博客页,然后链接 http://example.com/title-of-博客,而不是在页面上展示博客的细节,显示了我的错误信息近标题,博客的无网点。

我的问题是Apache是​​无法确定何时重写 cityres 页,当重写 blogdetail 页。

有人提出,确保每个规则都有一个共同的preFIX(例如/博客/第1页和/新闻/第2页)。,但我没有得到这

在这里有什么建议吗?


编辑:
全htaccess是如下

 选项+了FollowSymLinks
RewriteEngine叙述上

的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /index\.php
重写规则^指数\ .PHP $ / [L,R = 301]

的RewriteCond%{THE_REQUEST} ^ [A-Z] {3,9} \ /指数
重写规则^指数\ .PHP $ / [L,R = 301]

的RewriteCond%{HTTP_HOST} ^例如\ .COM $ [NC]
重写规则^(。*)$ http://www.example.com/$1 [L,R = 301]

#从URL中删除的.php
的RewriteCond%{} REQUEST_FILENAME -f .PHP
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)$ 1 \ .PHP [L]

#删除的.html
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] +)\。HTML / $ 1 [L,R = 301]

ErrorDocument的404 /错误页面
的ErrorDocument 403 /错误页面

重写规则^食品 - ([^  ​​- ] *) - ([^  ​​- ] *)\ HTML $ / pdetail RES_ID = $ 1安培;?位置= $ 2 [L]
重写规则^美食家 - ([^  ​​- ] *) - ([^  ​​- ] *)$ / pdetail_new RES_ID = $ 1安培;位置= $ 2 [L]
重写规则^([^ /] +)/([^ /] +)/([^ /] +)/ $ / pdetail_ne 1位置= $&放大器;?RES_ID = $ 2及名称= $ 3 [L]

重写规则^博客/([^/.]+)/?$ / blogdetail_fm?prmn = $ 1 [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond $ 1!cityres
重写规则^([^ /] +)/?$ / cityres?cityf = $ 1 [L]
 

解决方案

无论你的规则匹配完全相同的模式。因此,第一个规则将总是匹配,第二条规则不执行任何操作。

看一下第一条规则:

 重写规则^([^ /] +)/?$ / blogdetail?prmn = $ 1 [L]
 

这符合 http://example.com/title-of-blog 以及 http://example.com/city-名称

当你看到它,你可以告诉它需要由blogdetail处理,并需要由cityres处理,但正则表达式([^ /] +)认为他们既作为完全一样,并同时匹配。您的正则表达式不知道其中的差别,所以无论第一条规则是,这两个网址将获得相匹配的。

就像你说的,有人用preFIX建议。这样一来,正则表达式的知道的哪个是哪个:

 重写规则^城市/([^/.]+)/?$ / cityres?cityf = $ 1 [L]
重写规则^博客/([^/.]+)/?$ / blogdetail?prmn = $ 1 [L]
 

和你的URL看起来像:

  http://example.com/city/city-name
http://example.com/blog/title-of-blog
 

如果你真的挂了关于不加prefixes,你可以删除第二个preFIX:

 重写规则^城市/([^/.]+)/?$ / cityres?cityf = $ 1 [L]
重写规则^([^ /] +)/?$ / blogdetail?prmn = $ 1 [L]
 

让你有:

  http://example.com/city/city-name
http://example.com/title-of-blog
 


编辑:

您500服务器错误是由循环的规则造成的。您需要添加一个条件,使他们不能保持匹配的:

 重写规则^博客/([^/.]+)/?$ / blogdetail?prmn = $ 1 [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond $ 1!cityres
重写规则^([^ /] +)/?$ / cityres?cityf = $ 1 [L]
 

I have a page on my website that is getting generated dynamically to list all outlets based on cityf parameter and below is rewrite rule to convert it into SEO friendly URL and it is working pretty well.

RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL (http://example.com/title-of-blog)

RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]

Now the problem here i am facing that when someone visits blog page then the link http://example.com/title-of-blog instead of displaying blog detail on the page, displays my Error message that No outlets near title-of-blog.

I got the issue that Apache is not able to identify when to rewrite cityres page and when to rewrite blogdetail page.

Someone suggested that Make sure that each rule has a common prefix (e.g. /blog/page1 and /news/page2). but i did not get that.

Any suggestions here please?


EDIT:
Whole htaccess is as below

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L] 

# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301] 

ErrorDocument 404 /error-page
ErrorDocument 403 /error-page 

RewriteRule ^food-([^-]*)-([^-]*)\.html$ /pdetail?res_id=$1&location=$2 [L]
RewriteRule ^foodies-([^-]*)-([^-]*)$ /pdetail_new?res_id=$1&location=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /pdetail_ne?location=$1&res_id=$2&name=$3 [L]

RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

解决方案

Both your rules match the exact same pattern. Therefore, the first rule will always match and the second rule does nothing.

Looking at the first rule:

RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]

This matches http://example.com/title-of-blog as well as http://example.com/city-name

When you look at it, you can tell which needs to be handled by blogdetail and which needs to be handled by cityres, but the regex ([^/.]+) sees them both as exactly the same, and matches both. Your regex doesn't know the difference, so whatever the first rule is, both URL's will get matched by it.

Like you said, someone suggested using a prefix. That way, the regex knows which is which:

RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]

ANd your URLs will look like:

http://example.com/city/city-name
http://example.com/blog/title-of-blog

If you're really hung up about not adding prefixes, you can remove the second prefix:

RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]

So that you have:

http://example.com/city/city-name
http://example.com/title-of-blog


EDIT:

Your 500 server error is caused by the rules looping. You need to add a condition so that they won't keep matching:

RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

这篇关于重写规则的htaccess影响其他重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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