andrew_biggart Posted May 13, 2013 Share Posted May 13, 2013 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=5NEW URL: http://example.co.uk/blog/?tag=twitter&paged=2NEW 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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/ Share on other sites More sharing options...
requinix Posted May 13, 2013 Share Posted May 13, 2013 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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429885 Share on other sites More sharing options...
jazzman1 Posted May 13, 2013 Share Posted May 13, 2013 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: Quote http://example.co.uk/blog/?tag=facebook&paged=5 Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429913 Share on other sites More sharing options...
andrew_biggart Posted May 14, 2013 Author Share Posted May 14, 2013 On 5/13/2013 at 7:15 PM, requinix said: 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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429973 Share on other sites More sharing options...
jazzman1 Posted May 14, 2013 Share Posted May 14, 2013 On 5/14/2013 at 9:52 AM, andrew_biggart said: 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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429984 Share on other sites More sharing options...
andrew_biggart Posted May 14, 2013 Author Share Posted May 14, 2013 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=5NEW URL: http://example.co.uk/blog/?tag=twitter&paged=2NEW 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 OnRewriteCond %{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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429990 Share on other sites More sharing options...
andrew_biggart Posted May 14, 2013 Author Share Posted May 14, 2013 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/ Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429991 Share on other sites More sharing options...
jazzman1 Posted May 14, 2013 Share Posted May 14, 2013 On 5/14/2013 at 11:10 AM, andrew_biggart said: 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] Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429993 Share on other sites More sharing options...
andrew_biggart Posted May 14, 2013 Author Share Posted May 14, 2013 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 https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429998 Share on other sites More sharing options...
adamfaux85 Posted May 14, 2013 Share Posted May 14, 2013 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! Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1429999 Share on other sites More sharing options...
jazzman1 Posted May 14, 2013 Share Posted May 14, 2013 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] Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1430000 Share on other sites More sharing options...
andrew_biggart Posted May 14, 2013 Author Share Posted May 14, 2013 Thanks for the advice / help jazzman1. Link to comment https://forums.phpfreaks.com/topic/277958-htaccess-re-writing-a-url-using-multiple-parts-of-the-original/#findComment-1430011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.