-1

I am wondering how to rewrite this url profile.php?user=1 to /user/1. I know that this can be done by .htaccess. Here is what i have tried and it worked fine but the only problem is that it

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]

Besides these codes, my .htacces also has these lines

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes 

RewriteCond %{THE_REQUEST} \s/+page\.php\?pagename=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?pagename=$1 [L,QSA]

Please also tell me if my .htaccess page is correct. I am not good at htaccess

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
anamika
  • 43
  • 6

2 Answers2

1

Change this:

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]

to:

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

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

And make sure the page has either absolute links or add this to the page's header:

<base href="/" />

otherwise, all your links will be broken.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

In htacess rewrite you want to in fact redirect a url structure to a different url

If you have a link to profile.php?user=1 you will have to change this like to this : profile.php/user/1 then tell your htacess to redirect this adresse to this profile.php?user=1

You can do this :

RewriteRule ^user/(.*)$        index.php?&user=$1

If you want to test out some htaccess I will recomande this usedul site : http://htaccess.madewithlove.be/

Sebastien B.
  • 476
  • 3
  • 10