Forum Moderators: phranque

Message Too Old, No Replies

swap vars

swap vars in htaccess

         

flapjack

12:54 am on Nov 25, 2009 (gmt 0)

10+ Year Member



Hi

I'm trying to get this code

# redirect client requests for old dynamic category urls to new friendly urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?blockpage=([^\ ]*)&cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%2-category-page%1?\.html [R=301,L]

to give me this result

http://www.example.com/category-page2.html

unfortunately it gives me a 404 page with a question mark in the url.

http://www.example.com/category-page2?.html

Is there any way of getting it to generate the url without the question mark. thanks

Here's the original htaccess code in it's entirety in case anything is conflicting.

# redirect client requests for old dynamic urls to new friendly urls (with pagination fix)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1-category-page1.html? [R=301,L]

# redirect client requests for old dynamic category urls to new friendly urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?blockpage=([^\ ]*)&cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%2-category-page%1?\.html [R=301,L]

#redirect client requests for old dynamic category urls to new friendly urls (with pagination fix)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?cat=([^\ ]*)&blockpage=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1-category-page%2?\.html [R=301,L]

# redirect client requests for index.php to http://www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

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

# I cant remember for the life of me why I added these
RewriteRule ^-\.html$ index.php [L]
RewriteRule ^dummy\.html$ index.php [L]

# rewrite friendly url to dynamic filepath for category pages (prevent dupe page 1 pagination)
RewriteRule ^([^-]+)-category-page1.html$ index.php?cat=$1 [L]

# rewrite friendly url to dynamic filepath for article pages
RewriteRule ^article-([0-9]+)-(.*)\.html$ index.php?article=$1&name=$2

# rewrite friendly url to dynamic filepath for main pages pages
RewriteRule ^([0-9]+)-(.*)\.html$ main-pages.php?pageid=$1&pagename=$2

# rewrite friendly url to dynamic filepath for category pages
RewriteRule ^([^-]+)-category-page([0-9]+).html$ index.php?cat=$1&blockpage=$2 [L]

jdMorgan

4:06 am on Nov 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking at the rule you posted, I see a question mark specified in that position in the substitution URL, so that's where that question mark is coming from.

If you don't see the question mark preceding "\.html", check your character-encoding settings, and use only a plain-text editor for working on Apache config code. Also, that backslash is not needed, since the substitution URL is not a regular-expressions pattern...

Jim

flapjack

10:09 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



Thanks JD

I found the root of the problem, I forgot to escape &cat in two of the rules.

jdMorgan

2:33 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not entirely clear, but you should not have to escape "&cat=", as both "&" and "=" are allowed by the HTTP protocol to be clear-text in query strings...

Jim

g1smd

11:55 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The two characters in bold should be removed:
page%2[b]?\[/b].html
from multiple rules.

flapjack

10:40 am on Dec 3, 2009 (gmt 0)

10+ Year Member



thanks gents

I had a wee problem with the query string appended onto the urls but after some digging found that adding a ? removes it.

I finally settled on these two working rules

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?cat=([^\ ]*)&blockpage=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1-category-page%2.html? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?blockpage=([^\ ]*)&cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%2-category-page%1.html? [R=301,L]

thanks again guys