0

How do I go about using Apache .htaccess to rewrite "example.com/user.php?username=test" to "example.com/user/test"?

I'm currently working with the following:

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

But this seems to treat "user/" as a subfolder; it then appears to look for "$1" within it (which doesn't exist obviously) and then redirects me to "user/index.php".

Currently my .htaccess file looks like:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R,L]

This adds HTTPS, WWW and removes any trailing slash.

Any help would be great, thanks!

  • Possible duplicate of [How can I use .htaccess to hide .php URL extensions?](https://stackoverflow.com/questions/10028025/how-can-i-use-htaccess-to-hide-php-url-extensions) – KennyDaren May 20 '19 at 11:17
  • Though the rules you use can be enhanced they _should_ work... So there has to be some other issue, not the rules themselves. Please try to add more details to your question: entries in the http servers error log file, enable rewrite logging and check what is happening inside your rewrite engine, test using an anonymous browser window. – arkascha May 20 '19 at 16:22
  • Thank you for your help. I've noticed that, because I have an actual folder called "users", htaccess seems to want to treat "user" as a real folder too. If I change this to "who" (for example), it suddenly works. Though now all of my relative links have been appended with "who"... This is the quote from http://httpd.apache.org/docs/current/mod/mod_rewrite.html: " Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system...". – John.Ranger May 20 '19 at 16:37

0 Answers0