0

I have tried many rewrite rules for the following url but i didn't get any success.I researched a lot and wasted my 2 days.

I need this url to be like this I need to rewrite url from

http://localhost/tour/tour.php?page=golden-triangle-tour

to

http://localhost/tour/tour/golden-triangle-tour

Please guide me to fix this in htaccess.

5
  • "I have tried many rewrite rules" what are? Commented Jun 14, 2015 at 7:32
  • Just to be clear, you want the url with the .php in it to be rewritten to the lower url? Or redirected? Commented Jun 14, 2015 at 7:33
  • This looks like you miss understood the concept of rewriting. Rewriting does not allow you to somehow magically convert the output of your application so that links look better or are more search engine friendly. You have to do that yourself in your logic. Rewriting allows to to rewrite incoming requests to your http server such that you can translate some url like the second version you posted to the internal version like the first example you posted which can be handled by your script. So (usually) rewriting is used to convert the second form to the first, so opposite of your request. Commented Jun 14, 2015 at 7:39
  • possible duplicate of how to use htaccess to chance the URL...? Commented Jun 14, 2015 at 7:47
  • This question is different .. otherwise i wouldn't be asking here twice brother.www.site.com/abc.php#123.php to www.site.com/123.php UPDATE: Ok, how can I do this then www.site.com/abc.php to www.site.com/abc/ Commented Jun 14, 2015 at 7:56

2 Answers 2

2

You can use this code in your /tour/.htaccess file:

RewriteEngine On
RewriteBase /tour/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /tour\.php\?page=([^\s&]+) [NC]
RewriteRule ^ tour/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /tour/(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^tour/([^/.]+)/?$ tour.php?page=$1 [L,QSA,NC]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/toure/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Sign up to request clarification or add additional context in comments.

7 Comments

Many many Thanks.It worked like a charm.You are really a geek.
hello .I am really happy but i need all sites url to be like this. localhost/tour/about.php to localhost/tour/about.. can you make edits to existing one.I will mark you as accepted .
No it says 404 error.I need just url like localhost/tour/about.php to localhost/tour/about
But that is a different question from what you asked above. It is not good practice to add new questions via comments. My previous answer was for the original question you asked.
ya your previous question is really helpful..and i fixed that in my your edited htaccess..now it worked fine.thanks
|
0

Another solution :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC] 
RewriteRule ^tour.php$ /tour/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tour/([^/]+)/?$ /tour.php?page=$1 [QSA,L,NC]

1 Comment

Actually this will cause infinite looping since both rules will keep triggering each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.