manlikeangus
manlikeangus

Reputation: 421

Configure .htaccess to allow Vanity URLs

I an htaccess file to help me force to "www.", add "https://" if it is turned on and also remove the ".php" from files and also add a trailing forward slash "/" that has been set up like this:

Options -Indexes

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php [L]

RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]

Now, I want to make it possible to have vanity URLs. So far, I have:

RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?user=$1 [L]</pre>

How can I have use all of it together? I tried adding the vanity URL bit and I started getting a 500 error.

Upvotes: 1

Views: 92

Answers (1)

anubhava
anubhava

Reputation: 785481

Have it like this:

Options -Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

# ignore files/directories from below rules
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L,NE]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1/$2\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]

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

Upvotes: 2

Related Questions