简体   繁体   English

RewriteRule阻止RewriteCond和RewriteRule之后

[英]RewriteRule blocking RewriteCond & RewriteRule after it

If this is a duplicate then I am really sorry, but I used google and also search here and could not find anything that would work for me. 如果这是重复,那么我真的很抱歉,但我使用谷歌,也在这里搜索,找不到任何适合我的东西。 Maybe I just don't know what exactly to search for. 也许我只是不知道究竟要搜索什么。

Anyway, back to my question: 无论如何,回到我的问题:

I am trying to change old ugly URLs to nicer urls (wanted to have news/XXX/XY but relative links defeated me), so I wrote couple lines of code to my .htaccess and tested them (well, I actually used same code on my other page for redirect, so I know it does work). 我正在尝试将旧丑陋的URL更改为更好的URL(想要有news/XXX/XY但相对链接打败了我),所以我写了几行代码到我的.htaccess并测试它们(好吧,我实际上使用相同的代码我的另一个重定向页面,所以我知道它确实有用)。 I get redirected from /news.php to /news , but the query string magic is not working at all... 我从/news.php重定向到/news ,但查询字符串魔法根本不起作用...

RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]

RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]

RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]

RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]

None of the RewriteRules after first one gets executed. 第一个执行后的RewriteRules都没有执行。 But if I change my .htaccess like this: 但如果我像这样改变我的.htaccess:

RewriteRule ^news.php$ /news [L,NC,R=301]

RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]

RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]

RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]

RewriteRule ^news?$ /news.php [L,NC]

then it works. 然后它工作。 I tried using /\\?bla - /news\\?bla - /news\\.php\\?bla etc. Nothing actually worked. 我尝试使用/\\?bla - /news\\?bla - /news\\.php\\?bla等。没有任何实际工作。

Does somebody have any Idea why its behaving like this and how to fix it? 有人有任何想法为什么它的表现如此以及如何解决它? It looks like it does not recognize the news after it starts loading from news.php file. news.php文件开始加载后,它似乎无法识别news

I am completly lost, as I don't work with .htaccess that often. 我完全迷失了,因为我经常不和.htaccess合作。 Any ideas? 有任何想法吗?

I actually ended up using this: 我实际上最终使用了这个:

#########################
#   NICE URLS QUERIES   #
#########################

RewriteCond %{THE_REQUEST} /news(?:\.php|)\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-page-%1? [R=302,L]

RewriteCond %{THE_REQUEST} /news\(?:\.php|)\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]

RewriteCond %{THE_REQUEST} /news(?:\.php|)\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]

RewriteRule ^news-([0-9]+)-([0-9]+)?$ news.php?readmore=$1&rowstart=$2 [L,QSA,NC]
RewriteRule ^news-([0-9]+)/?$ news.php?readmore=$1 [L,QSA,NC]
RewriteRule ^news-page-([0-9]+)/?$ news.php?rowstart=$1 [L,QSA,NC]


#######################
#   OTHER NICE URLS   #
#######################

RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]

The first thing you need to make sure you're doing is putting all the redirects first. 确保您正在做的第一件事就是将所有重定向放在第一位。 That is, every rule that has the R flag needs to be before the rules that don't. 也就是说,具有R标志的每个规则都需要在不具有R标志的规则之前。 But it looks like you're not matching the - style URLs to rewrite them BACK to the version with the query string. 但看起来你没有匹配-样式的URL来重写它们返回到带有查询字符串的版本。 So I think you really want something like this (you can get rid of the .php extension while you get rid of the query string): 所以我认为你真的想要这样的东西(当你摆脱查询字符串时你可以摆脱.php扩展名):

RewriteCond %{THE_REQUEST} \ /+news\.php(\ |$)
RewriteRule ^ /news [L,R=301]

RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?rowstart=([\ &]+)
RewriteRule ^ /news-row-%1? [L,R=301]

RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([^&]+)&rowstart=([\ &]+)
RewriteRule ^ /news-%1-%2? [L,R=301]

RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([\ &]+)
RewriteRule ^ /news-%1? [L,R=301]

## Now, internally rewrite the request back to the query string and php extension

RewriteRule ^news$ /news.php [L]
RewriteRule ^news-row-(.*)$ /news.php?rowstart=$1 [L,QSA]
RewriteRule ^news-(.*)-(.*)$ /news.php?readmore=$1&rowstart=$2 [L,QSA]
RewriteRule ^news-(.*)$ /news.php?readmore=$1 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM