Jump to content

.htaccess - re-writing a url using multiple parts of the original


Go to solution Solved by andrew_biggart,

Recommended Posts

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.

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!

Link to comment
Share on other sites

You call them "old" and "new" and that seems the complete opposite of what it looks like you want to do.

 

Are you trying to use links like /blog/?tag=facebook/page/5/ and make them behave like /blog/?tag=facebook&paged=5?

Link to comment
Share on other sites

Assuming that your web root is named: "example.co.uk"

Inside a root directory you have a sub-directory named: "/bog" wich contains for that example debug.php file.

Inside debug.php file you have next links:

 

/blog/debug.php

<a href="./debug.php?tag=facebook/page/5">Some url 1</a>

<a href="./debug.php?tag=twitter/page/5">Some url 2</a>

<a href="./debug.php?tag=google/page/5">Some url 3</a>

<!-- 

http://example.co.uk/blog/?tag=facebook&paged=5 

http://example.co.uk/blog/?tag=facebook/page/5/

-->

Now, you need to create a .htaccess file which should be locate inside "/blog" directory and copy/paste next content: 

 

/blog/.htaccess

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

If we try to reach the first link and redirect it to a new URL, see the result:

 

Results:

 

 

Edited by jazzman1
Link to comment
Share on other sites

You call them "old" and "new" and that seems the complete opposite of what it looks like you want to do.

 

Are you trying to use links like /blog/?tag=facebook/page/5/ and make them behave like /blog/?tag=facebook&paged=5?

 

 Yes this is exactly what I'm trying to do.

 

 

jazzman1

 

Your suggested method didn't redirect as expected. It just forwarded me to a 404 page...

Link to comment
Share on other sites

 Yes this is exactly what I'm trying to do.

 

 

jazzman1

 

Your suggested method didn't redirect as expected. It just forwarded me to a 404 page...

 

It should be work, I've tested it on my local server before posting.

 

How looks like your links:

http://example.co.uk/blog/?tag=facebook/page/5/

//or

http://example.co.uk/blog/?tag=facebook/page/5

Also, do you have integers in the end like /10/, /20/, /133/

Link to comment
Share on other sites

Amended issue.

 

 

 

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!

Link to comment
Share on other sites

jazzman1 What is your local setup? Apache or FastCGI?

 

The links are in the following format.

 

http://example.co.uk/blog/?tag=facebook/page/5/

 

 

They also have integers which can vary.

 

The live url link is as follows if you want to check it out.

 

http://peppermintsoda.co.uk/blog/

 

 

I'm using an Apache server, so my last RegEx is wrong b/s I forgot to add "/" symbol.

 

Also, make sure about name of the file in that case I'm using debug.php.

 

Well, well, well where is the file contains your links? Is it a index file or ... ?

 

Try that:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/blog/debug\.php$
RewriteCond %{QUERY_STRING} ^tag=([a-z]+)/page/([0-9]+)/$
RewriteRule ^(.*)$ http://example.co.uk/blog/?tag=%1&paged=%2 [R,L]
Edited by jazzman1
Link to comment
Share on other sites

  • Solution

Jaxxman1 you're my hero. I had to remove the debug.php part, but it worked a treat. Thanks so much for your help.

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]
Link to comment
Share on other sites

Also, there is no reason to use full redirection like "http://example.co.uk/blog/?tag=%1&paged=%2" on the same web root and machine (server). 

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/blog/$
RewriteCond %{QUERY_STRING} ^tag=([a-z]+)/page/([0-9]+)/$
RewriteRule ^(.*)$ /blog/?tag=%1&paged=%2 [R,L]
Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.