Eggo
Eggo

Reputation: 539

Rewriting in .htaccess with forward slashes

here is what I need. I have the url pearlsquirrel.com/profilecomments.php?u=eggo. eggo being my username and the part of the dynamic url that changes. I would like to rewrite the url to say pearlsquirrel.com/eggo/comments, using .htaccess.

Here is what I have so far:

RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301]

but I just can not get it to work. Any help would be greatly appreciated. Thanks!

Upvotes: 1

Views: 188

Answers (2)

Tom
Tom

Reputation: 3520

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L]

NOTE

If you've used relative paths for your images, stylesheets etc. you need to change those into absolute paths or relative paths using the servers root folder as base in order to get your site to display properly.

For example, it will think that images/image.png is /eggo/comments/images/image.png

But, if you instead add a preceding slash /images/image.png your file paths will always start from the servers root folder and your site won't get messed up when you're rewriting your URL's.

Upvotes: 1

Ala' Ibrahim
Ala' Ibrahim

Reputation: 21

Your first rule, overrides all the rest. What you are describing (if I understood correctly), you need to handle /user/comments by profilecomments.php?u=user

RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L]

this should do it.

Upvotes: 0

Related Questions