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]