Question

I've been trying to fix this problem for 3 days and can't find the answer so I'm hopeful someone will be able to shed some light on this.

I am looking to dynamically re-write URLs as permanent 301 re-directs for SEO purposes using .htaccess. I'm looking to change the following:

OLD URL EXAMPLE: http://example.co.uk/blog/?tag=facebook/page/5/
OLD URL EXAMPLE: http://example.co.uk/blog/?tag=twitter/page/2/
OLD URL EXAMPLE: http://example.co.uk/blog/?tag=google/page/12/

to

NEW URL: http://example.co.uk/blog/?tag=facebook&paged=5
NEW URL: http://example.co.uk/blog/?tag=twitter&paged=2
NEW URL: http://example.co.uk/blog/?tag=google&paged=12

I need to be able to re-use two dynamic properties, the tag e.g.('facebook') and the pagination number e.g.('5') for each url request that comes in as these could be different with each request.

The reason for this is because we are trying to resolve 404 errors from our old wordpress blog that didn't use pagination, just 'next' and 'previous', which displayed as '/page/5'. The new blog uses pagination which displays as '&paged=5'.

This is an example of what I've tried.

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/blog/$
RewriteCond %{QUERY_STRING} ^tag=([a-z]+)/page/([0-9])$
RewriteRule ^(.*)$ http://example.co.uk/blog/?tag=%1&paged=%2 [R,L]

PHP is running as CGI, and can't change to apache, causes lots of permission issues across the site.

I've tried a number of combinations of RewriteRules, RewriteConds and %{QUERY_STRING} requests and still no joy. Any help with this would be greatly appreciated!

Was it helpful?

Solution 2

Thanks for your help guys we tried both of your methods but they didn't work. Eventually we managed to get it right after some more trial and error. Here's what ended up working for us.

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/blog/$
RewriteCond %{QUERY_STRING} ^tag=([a-z]+)/page/([0-9]+)/$
RewriteRule ^(.*)$ http://example.co.uk/blog/?tag=%1&paged=%2 [R,L]

Note: The were just a few syntax errors that seems would work on an Apache PHP setup but needed tweaking for a CGI PHP setup.

We also re-used this htaccess code to account for categories too by changing 'tag' for 'cat' and changing the regular expression for the cat from letters to numbers:

RewriteCond %{REQUEST_URI}  ^/blog/$
RewriteCond %{QUERY_STRING} ^cat=([0-9]+)/page/([0-9]+)/$
RewriteRule ^(.*)$ http://example.co.uk/blog/?cat=%1&paged=%2 [R,L]

Thanks again.

Hope this can help someone in future with the same issue.

Now on to the others! :)

OTHER TIPS

This should work:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^tag=([^/]+)/page/([^/]+)/?$ [NC]
RewriteRule ^(blog)/?$ /$1/?tag=%1&paged=%2 [R=302,L,NC]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top