Jump to content


Photo

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

htaccess rewrite

Best Answer andrew_biggart, 14 May 2013 - 06:35 AM

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]
Go to the full post


  • Please log in to reply
11 replies to this topic

#1 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 13 May 2013 - 11:21 AM

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...acebook/page/5/
OLD URL EXAMPLE: http://example.co.uk...twitter/page/2/
OLD URL EXAMPLE: http://example.co.uk...google/page/12/

 


to

NEW URL: http://example.co.uk...acebook&paged=5
NEW URL: http://example.co.uk...twitter&paged=2
NEW URL: http://example.co.uk...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!


while($succeed != true) { $action = 'try'; }


#2 requinix

requinix

    Hopeless Member

  • Moderators
  • 5,911 posts
  • LocationWA

Posted 13 May 2013 - 02:15 PM

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?

#3 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 13 May 2013 - 03:39 PM

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:

 

 


http://example.co.uk...acebook&paged=5


Edited by jazzman1, 13 May 2013 - 03:50 PM.


#4 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 14 May 2013 - 04:52 AM

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...


while($succeed != true) { $action = 'try'; }


#5 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 14 May 2013 - 05:59 AM

 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/



#6 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 14 May 2013 - 06:08 AM

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...acebook/page/5/
OLD URL EXAMPLE: http://example.co.uk...twitter/page/2/
OLD URL EXAMPLE: http://example.co.uk...google/page/12/


to

NEW URL: http://example.co.uk...acebook&paged=5
NEW URL: http://example.co.uk...twitter&paged=2
NEW URL: http://example.co.uk...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...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!


while($succeed != true) { $action = 'try'; }


#7 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 14 May 2013 - 06:10 AM

jazzman1 What is your local setup? Apache or FastCGI?

 

The links are in the following format.

 

http://example.co.uk...acebook/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/


while($succeed != true) { $action = 'try'; }


#8 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 14 May 2013 - 06:18 AM

jazzman1 What is your local setup? Apache or FastCGI?

 

The links are in the following format.

 

http://example.co.uk...acebook/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, 14 May 2013 - 06:23 AM.


#9 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 14 May 2013 - 06:35 AM   Best Answer

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]

while($succeed != true) { $action = 'try'; }


#10 adamfaux85

adamfaux85

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 14 May 2013 - 06:36 AM

Double checked and your solution works perfectly using other tags and page numbers! You are a legend! Thanks so much for your input on this! You deserve a pint!



#11 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 14 May 2013 - 06:50 AM

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, 14 May 2013 - 06:50 AM.


#12 andrew_biggart

andrew_biggart

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationManchester

Posted 14 May 2013 - 08:26 AM

Thanks for the advice / help jazzman1.


while($succeed != true) { $action = 'try'; }





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com