Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.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
.htaccess 重写规则和其他重写规则_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess 重写规则和其他重写规则

.htaccess 重写规则和其他重写规则,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我在我的网站上有一个页面,根据cityf参数动态生成,列出所有门店,下面是重写规则,将其转换为SEO友好的URL,运行良好 RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L] 我在我的网站上有一个博客页面。htaccess如下所示,用于转换SEO友好的URL(http://example.com/title-of-blog) 现在我面临的问题是,当有人访问blog页面时,链接http://example.com/title-of-blog不在页面上显示

我在我的网站上有一个页面,根据
cityf
参数动态生成,列出所有门店,下面是重写规则,将其转换为SEO友好的URL,运行良好

RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
我在我的网站上有一个博客页面。htaccess如下所示,用于转换SEO友好的URL(
http://example.com/title-of-blog

现在我面临的问题是,当有人访问
blog
页面时,链接
http://example.com/title-of-blog
不在页面上显示博客详细信息,而是显示我的错误消息,
博客标题附近没有出口

我发现Apache无法确定何时重写
cityres
页面以及何时重写
blogdail
页面

有人建议
确保每个规则都有一个共同的前缀(例如/blog/page1和/news/page2)。
但我没有明白

有什么建议吗


编辑:
整个访问过程如下所示

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]

您的两条规则完全匹配相同的模式。因此,第一条规则将始终匹配,而第二条规则不执行任何操作

看看第一条规则:

RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
这与
http://example.com/title-of-blog
以及
http://example.com/city-name

当您查看它时,您可以知道哪些需要由blogdetail处理,哪些需要由cityres处理,但是regex
([^/]+)
将两者视为完全相同,并且两者都匹配。你的正则表达式不知道有什么区别,所以不管第一条规则是什么,两个URL都会被它匹配

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

RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
您的URL将如下所示:

http://example.com/city/city-name
http://example.com/blog/title-of-blog
如果您真的对不添加前缀感到不安,可以删除第二个前缀:

RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
因此,您有:

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

编辑:

您的500服务器错误是由规则循环引起的。您需要添加一个条件,以便它们不会保持匹配:

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

很好的详细答案+1,祝贺你获得了10万:)@JonLin。。我尝试了
RewriteRule^blog/([^/]+)/?$/blogdeail\u fm.php?prmn=$1[L]RewriteRule^([^/]+)/?$/cityres?cityf=$1[L]
但现在我在博客页面上出现了500个错误:(现在我的Htaccess看起来像
RewriteRule^([^/]+)/?$/cityres\cityres=$1[L]RewriteCond%{请求文件名}!-f rule^blog/)/[五十] 
但仍然不走运。@JonLin..我添加了我的完整htaccess,因为我仍然面临500@Gags当我设置cityres.php和blogtail_fm.php文件时,这些规则对我来说很好。您的错误日志怎么说?
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]