Help a htaccess dummy please

Joined
Dec 17, 2015
Messages
31
Likes
25
Degree
0
I'm hoping some kind-hearted whiz can help me out. I have an old website i built running off a database of towns and cities generating listings. When I built it using my limited php knowledge I hacked it together simply passing the variables through the url. I want to fix the site up now including pretty URL's. Ive run it through an online htaccess generator which has given me the following but my problem is I have hundreds of URL's indexed already. Can anyone give me some help adding some code to 301 the old URL's the the new ones or point me to a similar tool which will help?

Thanks

Code
RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L]
 
^^ Took a cursory glance - that seems generic enough to work with those two types of URLs.

Do you just have more URLs like the two you posted (in which case you don't need to do anything), or do you have URLs which are of a third different pattern?
 
Thanks for the reply. The code example simply re-writes my URLs to a better format. I need to add some code which will 301 the existing indexed URL's to the re-written ones to avoid duplicate content issues. There are only two URL's as in the example. One shows content for all States (actually cities in the UK) and the other shows content for Cities in the selected State (actually UK towns in the selected city)
 
^ 302 is default, so that is true. Add [R=301] explicitly to the end.

"There are only two URL's as in the example."

Then you should be all set. Something like this should work:

RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L, R=301]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L,R=301]
 
I may be confused but you said you want "Pretty URLs" as the new destinations. And the parameter passing URLs are the old ones. If that's the case then you have your URL's in the wrong order after RewriteRule.

RewriteRule ^old\.html$ new.html [R=301]​
 
Thank you everyone. Ryuzaki, that was exactly what i have been struggling with in my head. My example rewrites my old URL's to 'pretty' ones which works as I wanted. Like you say, 301 redirects work the other way round so I have been trying with the code i pasted followed by 2 more lines of code which are the same as the two above (see below) but have the rules reversed followed by [R=301]. This gave me server errors first then i got it working but the redirects still don't seem to work. If i click an indexed link in Google I still get taken to the old URL. Would you be doing what I am attempting by creating two sets of rules, one for re-write and then another reversed for 301?
CODE
RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L]
RewriteRule ^united-kingdom/keyword.php?state=$1&state_id=$2$ /united-kingdom/keyword/([^/]*)/([^/]*)/[R=301]
RewriteRule ^united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3$ /united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/[R=301]
 
I may be confused but you said you want "Pretty URLs" as the new destinations. And the parameter passing URLs are the old ones. If that's the case then you have your URL's in the wrong order after RewriteRule.

RewriteRule ^old\.html$ new.html [R=301]​

I could be wrong, but I don't think he added any additional code in the application to handle the "new" pretty URLs.

@butcher

Scenario 1
When you got/bought the site, as I understand it, the URLs used to be like these:
yourdomain.com/united-kingdom/loans-in.php?city=guildford&state=suffolk&state_id=22
yourdomain.com/united-kingdom/loans.php?state=suffolk&state_id=22

You want people to be able to type these URLs and reach those same pages.
yourdomain.com/united-kingdom/loans/guildford/suffolk/22
yourdomain.com/united-kingdom/loans/suffolk/22

You have not made any changes to the application.

Is that right?

If so, use the code I posted above. (Pasting it here )
RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L, R=301]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L,R=301]


Do you want the users (and their browsers) to be able to continue to request the same "pretty URLs"?
Then, remove the "=301" from both lines so that it is always treated as a temporary redirect. Like this:
RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L, R]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L,R]


From Apache's perspective, your new pretty URL is the "original URL" (or old URL) and your actual non-pretty URL (with the ugly ".php?") is the "destination URL" (or new URL).

From your users' perspective, they will request your new "pretty URL". The server will redirect it to your old non-pretty URL.

You can build links to the "new" pretty URLs. They will get redirected to the non-pretty URLs by your server.

In this case, the way redirects are written, your "old" is new/target/destination for the server. Your "new" is original/old for the server :smile:

Scenario 2

If your application, as a result of changes done by you (or someone else), now processes the pretty URLs without the need for any server redirect, then you DO NOT NEED any redirection.

Hope that is clear now.
 
Last edited:
Thank you @builder for taking time to respond. Sorry to keep asking stupid questions. Scenario2 is the correct one. I re-coded the site to use the new URL structure and using my original htaccess example it works correctly i.e the pretty URL's like yourdomain.com/united-kingdom/loans/guildford/suffolk/22 work fine. The problem I have is that the old ones also work, so I can access the same page using the above URL and the old one yourdomain.com/united-kingdom/loans.php?state=suffolk&state_id=22. So i need a way to redirect the old ones to the new ones to:
a. Remove duplicate page issues
b. To tell search engines that the old ones that are currently indexed should 301 to the new ones
 
If by "I re-coded the site to use the new URL structure" you meant "my site's code now processes pretty URLs and serves the right pages dynamically", then you don't need redirection.
You also said you are using the original .htaccess file and "it works correctly". So then it looks like now your application serves both URLs.

Do the following steps:

(1) Remove the redirection in .htaccess completely
(2) Restart your server (optional)
(3) Access yourdomain.com/united-kingdom/loans-in.php?city=guildford&state=suffolk&state_id=22
(4) Access yourdomain.com/united-kingdom/loans/guildford/suffolk/22

A) If (3) and (4) are served properly:

This means your site handles both URLs (pretty, non-pretty) correctly.
In this case, you want to tell search engines and browsers not to use non-pretty version anymore.
So, add this to your .htaccess:

RewriteEngine On
RewriteRule ^united-kingdom/keyword.php?state=([^/]*)&state_id=([^/]*)$ /united-kingdom/keyword/$1/$2/[L, R=301]
RewriteRule ^united-kingdom/keyword-in.php?city=([^/]*)&state_id=([^/]*)&state=([^/]*)$ /united-kingdom/keyword/$1/$2/$3/[L, R=301]

(I hope you can see the differences - in your original, you backtraced the second part which didn't make sense)

Build links to your new pretty URLs.

B) If (3) is served properly, but (4) gives 404 (or whatever the server code generates):

This means your site does not handle the new pretty URL, contrary to what you think. In this case, you need to decide what you want to do.
Changing your code is one option.
The other option if you want users to be able to request for the new pretty URLs (without changing any code) is:
RewriteEngine On
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/$ /united-kingdom/keyword.php?state=$1&state_id=$2 [L, R=301]
RewriteRule ^united-kingdom/keyword/([^/]*)/([^/]*)/([^/]*)/$ /united-kingdom/keyword-in.php?city=$1&state_id=$2&state=$3 [L,R=301]

Using 301 gives you a little bit of wriggle room with search engines. Build links to your new pretty URLs.


C) If (4) is served properly, but (3) gives 404 (or whatever the server code generates):

This means your site handles the new pretty URLs, but do not handle the old URLs anymore.
In this case, add this to your .htaccess:
RewriteEngine On
RewriteRule ^united-kingdom/keyword.php?state=([^/]*)&state_id=([^/]*)$ /united-kingdom/keyword/$1/$2/[L, R=301]
RewriteRule ^united-kingdom/keyword-in.php?city=([^/]*)&state_id=([^/]*)&state=([^/]*)$ /united-kingdom/keyword/$1/$2/$3/[L, R=301]

D) Neither (3) nor (4) works

You really should learn more before doing anything of this sort.

I wish I could write a blurb about how the whole thing works, but am too tired.
If you still have problems, PM me.
 
@builder

Sorry, i've been away for a few days. I got what I wanted in the end. I couldn't achieve what i wanted purely from htaccess so I used the original code to process the URL rewrites which gave me my 'pretty' URL's then I wrote a little php script to return a 301 header re-direct dynamically for the old URL's so I still benefit from old incoming links and search traffic from the old indexed pages. I'm not sure its the slickest solution but it does what i need and i move on. Thanks for your help in this thread.
 
Back