scott
scott

Reputation:

mod_rewrite redirects screw up paths

Here's a redirect I have:

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

The problem is if I type http://www.example.com/apple it's OK when it comes to my links on the page.

BUT if I use http://www.example.com/apple/ (notice last slash) then links are all screwed up.

How do I write the .htaccess so links will not include the last slash and not screw up links on page?

Update: I ended up using:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$  /$1 [R=301,L]

Upvotes: 0

Views: 159

Answers (2)

dreadwail
dreadwail

Reputation: 15410

You can strip the trailing slash with the following first:

remove-those-trailing-slashes-from-your-uris

Then process as normal after that point.

Upvotes: 2

Gumbo
Gumbo

Reputation: 655369

You should consider using absolute URL paths or absolute URLs to reference external resources.

Because relative URLs are resolved from a base URL, that is the URL of the current documen if not declared otherwise (see BASE HTML element). So if you reference /baz/quux using just the relative URL path baz/quux it would be resolved correctly to /baz/quux when used in /foo, but it would be resolved to /foo/baz/quux when used in /foo/bar. But the absolute URL path /baz/quux is always resolved to /baz/quux.

Upvotes: 0

Related Questions