/    Sign up×
Community /Pin to ProfileBookmark

URL Rewrite Question – Trailing Slash

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_
-]+)/$ user.php?username=$1

Hi this works ok for:

[url]http://domain/user[/url]

but not for:

[url]http://domain/user/[/url]

What should I add to make the trailing slash at the end take?

Thanks.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@xeteaOct 25.2010 — change it to:

RewriteEngine on

RewriteRule ^([^/.]+)/?$ user.php?username=$1

The important part is the question mark after the slash. That means that there can, but doesn't have to, be a slash after the matched expression.

This is the best tutorial I have found on mod_rewrite: http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 25.2010 — Hey, thanks so much.

I tried your new code and when I added the trailing slash again (http://domain/user/) I lost my css style and such.
Copy linkTweet thisAlerts:
@xeteaOct 25.2010 — I have to admit I'm no expert in mod_rewrite. But if you post your HTML document I might find some answers.
Copy linkTweet thisAlerts:
@nodeheadOct 25.2010 — You page is using relative URLs. When you visit http://domain/user/ for example, most browsers will be looking for your css file at http://domain/user/CSS.css instead of http://domain/CSS.css (they'll think that "user" is a directory)

Add a Base tag to the head of your HTML document(s), that'll sort it out:

HTML

<base href="http://domain">

xHTML

<base href="http://domain" />
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 25.2010 — Yes, adding the base url in the head did the trick, thank you both very much!
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 25.2010 — .. uh oh, now if I try to go to any folder (ex: http://domain/folder/) it takes me to the mod rewrite setting trying to find a user with that folder name..
Copy linkTweet thisAlerts:
@xeteaOct 25.2010 — Yep, that would be the downside. It might be smarter to do something like this:

RewriteEngine on

RewriteRule ^user/([^/.]+)/?$ user.php?username=$1 [NC]

So to reach a user's page you would use the URL http://domain.com/user/username
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 25.2010 — Oh, that's a great idea. Thanks!
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 26.2010 — ..wait does the folder user/ have to actually EXIST for that to work?.. because it's not retrieving the url query string properly now..
Copy linkTweet thisAlerts:
@nodeheadOct 26.2010 — Just keep your link structure as you had it and use a rewrite condition to make sure a directory doesn't exist before implementing the rewrite rule:

[CODE]Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([a-z0-9_-]+)/?$ user.php?username=$1 [L,NC][/CODE]
Copy linkTweet thisAlerts:
@nodeheadOct 26.2010 — Incidently, xetea's point of using a fake directory to differentiate a user request in the URL [I]is[/I] a tidier option... but usually only if you've planned it out that way. And NO! The "user" folder shouldn't exist.
Copy linkTweet thisAlerts:
@theflyingminstauthorOct 26.2010 — Weird, cause the way that ended up working for me was xetea's:

RewriteEngine on

RewriteRule ^user/([^/.]+)/?$ user.php?username=$1 [NC]

..As well as creating an empty user/ folder..
×

Success!

Help @theflyingminst spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...