简体   繁体   中英

.htaccess remove question mark from url

I have a website that has an query string in the url that I want to rewrite. The url is domain.com/profile.php?user=sven and I want it to redirect to domain.com/profile/user/sven .

I have created a htaccess file that has the following rules:

RewriteEngine On
RewriteRule ^/?user/([^/]+)/?$ profile.php?user=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9\-\_]+)$
RewriteRule ^profile\.php$ domain.com/user/%1?/  [L,R=301]

This works but the url it redirects to is: domain.com/user/sven?/

My question is, can I remove the ? in the url?

Update your htaccess to following,

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?profile/(.*?)/?$ /profile.php?user=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?user=([^]+)
RewriteRule ^/?profile\.php$ /users/%1? [L,R=301]
</IfModule>

I hope this helps.

Probaly all the problem here:

RewriteRule ^profile.php$ domain.com/user/%1?/ [L,R=301]

This may fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM