0

My website has dynamic pages such as

http://www.example.com/detailpage.php?sn=1%20&&%20database=news
http://www.example.com/detailpage.php?sn=1%20&&%20database=article
http://www.example.com/detailpage.php?sn=1%20&&%20database=interview

and many other

I want to remove or hide query string variable from url and display as

http://www.krishisansar.com/detailpage/news/1
http://www.krishisansar.com/detailpage/article/1

Is it possible to change querystring as slash ? if not, how to remove & and make as

/news=1 or /news=2, 
/article=1 or /article=2 .

By this, I can validate from w3c.

1 Answer 1

2

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

Options -MultiViews
RewriteEngine On
RewriteBase /

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

RewriteRule ^detailpage/(\w+)/(\d+)/?$ detailpage.php?sn=$2&database=$1 [L,QSA,NC]
Sign up to request clarification or add additional context in comments.

6 Comments

ok check updated rule. Make sure this is in root .htaccess (a level above news)
Fantastic. you really saved my time. this worked perfectly for me. According your code, url is displayed as my wish, but print mysql error. I suppose last line RewriteRule ^detailpage/(\w+)/(\d+)/?$ detailpage.php?sn=$2database=$1 [L,QSA,NC] is missing & between sn=$2 and database=$1. When I add & error is solved but css rule is disturbed. I am confused, adding & is wrong or my internal structure is wrong ?
Ah that's right, I had & missing in last rule. I have fixed it now. For css/js issue add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's URL.
Nice answer. The original links were a mess.
Yeah ! <base href="/" /> works. @anubhava Thanks for your valuable time and continuous support
|

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.