I have a page where the user signs up and they're redirected to their profile. Their profile URL is: example.com/profiles/?username=sam, where sam can be any name. This works successfully, but i'm trying to make the URL cleaner. I want to make the URL look like this: example.com/profiles/sam Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} /profiles/?\?username=([^&\s]+) [NC]
RewriteRule ^ /profiles/%1? [L,R]
RewriteRule ^(?:profiles/)?([0-9]+)/?$ /profiles/?username=$1 [L]
SetOutputFilter DEFLATE
<IfModule mod_setenvif.c>
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>
<IfModule mod_headers.c>
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
profiles in example.com/profiles/ is a directory. Inside profiles is an index.php file, this is the PHP file that creates the dynamic page. My .htaccess file is located in the root folder. I am also using GoDaddy. GoDaddy states that: "Because enabling mod_rewrite is handled at a global level, you do not need to enable it in your httpd.conf file. You only need to add the desired code to the body of your .htaccess file. The .htaccess file containing the rewrite rules must be in the same directory as the target files."
With my .htaccess like that, it changes the URL from example.com/profiles/?username=sam to example.com/profiles/sam but the profile is not shown at all, instead a 404 error page is shown, meaning the page does not exist. The page should exist. Also, adding Options +Multiviews produces the same 404 error. Also with Options +Multiviews, some pages display the CSS instead of the PHP page.
How can I make it so that example.com/profiles/?username=sam is redirected to example.com/profiles/sam and make the profile page actually show up?
Have your rules like this in /profile/.htaccess
:
RewriteEngine On
RewriteBase /profiles/
RewriteCond %{THE_REQUEST} /\?username=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?username=$1 [L,QSA]
[0-9]+
after /profile/
, which won't match sam
..php
you should check it's existence first.