1

I have url's like following with query string. I want to remove query string from url and want to make clean url.

www.demo.com/following.php?user=hardik
www.demo.com/fans.php?user=john

This url's should be like

www.demo.com/hardik/following
www.demo.com/john/fans

OR should be like

www.demo.com/following/hardik
www.demo.com/fans/john

Is this possible with htaccess? I tried to find a loot in google but still no luck. Need help.

Update:

I need something like this

www.demo.com/user/hardik/following/
www.demo.com/user/john/fans/

I tried like this

RewriteRule ^user/([^?]*) following.php?user=$2/ [L,QSA]

3 Answers 3

1

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

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\w+)/(\w+)/?$ $2.php?user=$1 [L,QSA,NC]
Sign up to request clarification or add additional context in comments.

6 Comments

Q1. What is location of this .htaccess file? Q2. Are there more rules in this file? Q3. What happens when you enter: www.demo.com/following/hardik in browser?
file location is on right place on root. There is one rule for removing index.php. And i see page without css loading.
Page without css is different problem, you need to provide all details. For that just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /. You can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
I check in browser view source and see there is already full url in css/js
This is redirecting to 404. #RewriteRule ^user/([^*]+)/following following.php?user=$1 [L,QSA] this code working for me.
|
0

Try with this for a url like www.demo.com/following/hardik

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/?$ $1.php?user=$2

Comments

0

Finally this is working for me for my last requirement. May be help someone else.

RewriteEngine On
RewriteBase /

RewriteRule ^user/([^*]+)/following/$ following.php?user=$1 [L,QSA]
RewriteRule ^user/([^*]+)/fans/$ fans.php?user=$1 [L,QSA]

Comments

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.