开发者

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!


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜