3

I want to make the pages on my site more search engine friendly by turning the query strings into meaningful looking page names.

For example I have a page that is this:

/search.php?c=18

I want it to look like this:

/doctors/MainCategoryName-18/

Sometimes there is a second query string so it would need to work with just one or both. For example:

/search.php?c=18&s=217

Should look like

/doctors/MainCategoryName-18/SubCatrogyrName-217/

Here's what I've tried so far but I can't get it to work.

RewriteRule ^search.php?c\=(.*)* /doctors/general-$1 [NC,R=301,L]
RewriteRule ^doctors/[^?/]*-(\d+)\ /search.php?c=$1&s=$2 [QSA

Thanks so much for your help,

2

try this:

RewriteEngine On
RewriteRule ^doctors\/[\w\-]+\-(\d+)\/?$ search.php?c=$1 [L,QSA]
RewriteRule ^doctors\/[\w\-]+\-(\d+)\/[\w\-]+\-(\d+)\/?$ search.php?c=$1&s=$2 [L,QSA]
| improve this answer | |
  • Hey thanks That worked great. But what if someone types in /search.php?s=523, can I make it automatically redirect them to:/doctors/Cardiology-112/ – Will Feb 25 '12 at 17:07
  • It works really well. But what if the category name has 2 parts like this: /doctors/arthritis-specialists-111/ Some could even have 3 or 4 words as part of the category name. For SEO reasons I'd like to have the name separated by a dash. – Will Feb 25 '12 at 17:26
  • I've edited the regexp to also include the dashes, so you can put as many words as you want. and the answer to your first comment - yes you can, but you shouldn't - friendly url does not suppose to work in that direction... and it also can't "guess" the category's name in the htaccess - if you want it to happen - you need to write it inside your search.php – Yaron U. Feb 25 '12 at 19:44
1

Add this to your .htaccess file residing in your DocumentRoot.

RewriteEngine On
RewriteBase /

#To prevent Looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

#To redirect the converted URL to actual URL
RewriteRule ^doctors/MainCategoryName-(\d+)/$ serach.php?c=$1 [L]

RewriteRule ^doctors/MainCategoryName-(\d+)/SubCatrogyrName-(\d+)/$ serach.php?c=$1&s=$2 [L]

#To convert your URL
RewriteCond %{QUERY_STRING} ^c=(\d+)$ [NC]
RewriteRule ^serach.php /doctors/MainCategoryName-%1/? [L,R=301]

RewriteCond %{QUERY_STRING} ^c=(\d+)&s=(\d+)$ [NC]
RewriteRule ^serach.php /doctors/MainCategoryName-%1/SubCatrogyrName-%2/? [L,R=301]
| improve this answer | |
  • using RewriteCond %{ENV:REDIRECT_STATUS} 200 is very dangerous, because even if the URL is a redirection, you still may want to apply rewrite rules. If I were you I would avoid such thing. Maybe, to avoid loop, before a "potential looping rule", test if the status is not 200: RewriteCond %{ENV:REDIRECT_STATUS} !200 – Olivier Pons Feb 27 '12 at 14:43
  • @OlivierPons Ya I agree! But, I use it with care. Like you said, only when rules are not required to be applied again. – ThinkingMonkey Feb 27 '12 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy