Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding .htaccess

I am trying to change my url type with .htaccess but I have a few problems. I tried a few online tools but they even do not work for me. So here what I am trying to do;

I have pages like http://mydomain.com/profile.php?u=newuser and I want to make as this: http://mydomain.com/newuser but so far I could not achieve it.

Here also what I have tried;

Options +FollowSymLinks
RewriteEngine on

RewriteRule (.*) profile.php?u=$1

After making changes in .htaccess, do I also have to make any changes in my php files? Also, when try to open http://mydomain.com/newuser I noticed that some of my images on the page are disappearing, what would be the reason for that? Thank you so much guys!

like image 827
makyol Avatar asked Nov 21 '25 07:11

makyol


2 Answers

You've gotten quite far, but now you're rewriting every possible url in your domain to profile.php, including stuff like /images/logo.jpg for example.

The question is, what do you want to do with this? An easy way to go about is changing it to this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?u=$1

The added 'RewriteCond' causes the rewriteengine to only rewrite urls that don't exist on the server, so your images will show up fine.

Personally, I think it might be better to add a /profile/ prefix to all your profile urls:

Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*) profile.php?u=$1

This will allow you to add new rewrite rules in the future, if you need them; it will also not give you issues if one of your users decides to go for a username called 'profile.php' or anything else that clashes with the existing urls on the server.

like image 191
Evert Avatar answered Nov 23 '25 23:11

Evert


This is because you're sending every request to profile.php (.*). This is going to affect all the requests for images, resources, etc.

Add this line above your rule to exclude "real" resources:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
like image 27
Abel Mohler Avatar answered Nov 23 '25 23:11

Abel Mohler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!