0

I am trying to convert my URL in to SEO Friendly url. I do not have much knowledge about htaccess rewrite rules, URL is:

https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop

the URL should be

https://www.zesteve.com/Guntur/manjunath-cake-shop

I have tried this:

RewriteEngine On  
RewriteCond $1 ^search 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?q=$1 [L,QSA]

I got the result as

https://www.zesteve.com/search?loc=Guntur&q=manjunath-cake-shop

Edit

I am using jquery to redirect

 window.location.href = 'search?loc=' + location[0] + '&q='+ search_term;
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Asesha George
  • 2,232
  • 2
  • 32
  • 68

3 Answers3

2

If any body look for same solution. Here is my answer

First it should redirect and convert to SEO Friendly URL

# Redirect to SEO Friendly Url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:search\.php)?\?loc=([^\s]+)&q=([^\s]+)? [NC]
RewriteRule ^ /%1/%2? [R=301]

and now Set URL

RewriteCond $1 ^ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?loc=$1&q=$2 [L,QSA]

It solved my problem.

https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop

above URL is now

https://www.zesteve.com/Guntur/manjunath-cake-shop

I don't want to show search.php page in URL

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Asesha George
  • 2,232
  • 2
  • 32
  • 68
1
RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]
alaric
  • 987
  • 5
  • 10
  • do i need to modify any thing apart from adding this code to htaccess file – Asesha George Mar 24 '17 at 07:36
  • What have your results been when replacing the RewriteRule you mentioned with it? – alaric Mar 24 '17 at 07:45
  • no change its showing same url. please check the url https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop – Asesha George Mar 24 '17 at 07:47
  • i removed all previous rewriting rules. now its working when i give direct url but i am using window.location.href = 'search?loc=' + location[0] + '&q='+ search_term; to redirect . i need to modify this also to make it work – Asesha George Mar 24 '17 at 08:09
  • @Alaric Update your question with proper description, avoid code-only answers. –  Mar 24 '17 at 08:37
  • @Sneak I assume this is more than your opinion. Update your comment with consensus-driven references to your pronouncements... please. I'm anxious to learn but all I've seen is http://stackoverflow.com/help/how-to-answer with no such info. – alaric Mar 24 '17 at 08:52
  • 1
    Thank you very much for the answer. its working. apart from you answer i add redirect.RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:search\.php)?\?loc=([^\s]+)&q=([^\s]+)? [NC] RewriteRule ^ /%1/%2? [R=301] . now its working grate. but images are not working – Asesha George Mar 24 '17 at 09:28
  • 1
    @Alaric It's good that you provided the solution, great job. However, adding a comment/description to your answer makes it more likely for people running into the same problem to understand what you are doing and why you are doing it this way to learn from it, and makes it more likely that you receive upvotes for a good documented answer. That goes in general, not for this specific answer only. Check this thread for some good guidelines: https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers –  Mar 24 '17 at 12:16
1

Use this in your .htaccess file:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]

It will leave you with the URL: https://www.zesteve.com/Guntur/manjunath-cake-shop. Just make sure you clear your cache before you test this.

Joe
  • 4,877
  • 5
  • 30
  • 51
  • ya i know i tried this but i am using jquery to redirect to this page my actual url is https://www.zesteve.com/search?loc=Guntur&q=manjunath-cake-shop . this should be redirect to https://www.zesteve.com/Guntur/manjunath-cake-shop – Asesha George Mar 24 '17 at 08:54