简体   繁体   中英

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). I get redirected from /news.php to /news , but the query string magic is not working at all...

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. But if I change my .htaccess like this:

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.

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.

I am completly lost, as I don't work with .htaccess that often. 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. But it looks like you're not matching the - style URLs to rewrite them BACK to the version with the query string. 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):

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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