: Vanity URLs with .htaccess and PHP? I need to create a vanity URL redirect using htaccess and PHP. My requirements are: If a user writes example.com/users/foobar The client will be redirected
I need to create a vanity URL redirect using htaccess and PHP.
My requirements are:
If a user writes example.com/users/foobar
The client will be redirected to for example:
profile.php?user=foobar in either the /users/ directory or the root directory. Possibly in an /inc/ folder in the root directory.
A example.com to example.com redirect is also necessary.
The URL needs to stay same visually. I would like all of this in a single .htaccess file.
Optional:
I would also like (although not a requirement) the redirect to also be applied on example.com/foobar
I have not found any answers that does this.
More posts by @Sims2060225
1 Comments
Sorted by latest first Latest Oldest Best
First off the easiest one for www is this.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourwebsite.com$ [NC]
RewriteRule ^(.*)$ www.yourwebsite.com/ [R=301,L]
Now for some actual rewriting you should be able to use something like this.
RewriteCond !^(users)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?user= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ profile.php?user= [L]
But to be honest, in my experience your best bet is just to try for yourself and create for your specific needs. If you don't create it for yourself and just go copy pasting, any minor change you will need and you will get in trouble. There is enough of examples and explanations just through google or checking other questions here on stackexchange.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.