views:

112

answers:

3

RewriteRule ^([^/\.]+)/$ $1 [R] redirects from website.com/abc/ to website.com/home/user/www/abc

How do I redirect to the correct location? (website.com/abc)

A: 

The first part of the RewriteRule is the matching part, the second the replacement expression. For full-url RewriteRules they should start with a ^/ (slash), because a path after http://exampl.com always starts with the root-slash. To redirect http://example.com/abc/ to http://example.com/abc (remove the trailing slash) you can do this:

RewriteEngine On
RewriteRule ^/([^/]+/$ /$1 [R]
Abel
A ) was left out so I tried these three:RewriteRule ^/([^/])+/$ /$1 [R]RewriteRule ^/([^/]+)/$ /$1 [R]RewriteRule ^/([^/]+/)$ /$1 [R]I'm still redirected to example.com/home/user/www/abc instead of example.com/abc
admin
other users have commented to use the `L` to finish it (L for Last) and prevent other rewrite rules to pop in. Also, if your page includes a redirect, or there's a redirect in the header of your page, then you can try what you want in Apache but it won't help. Can you provide an example link where this happens?
Abel
A: 

It looks like the URL is already being rewritten to a filename and then you are rewriting the filename. This usually happens when the rewrite rule is being specified in an .htaccess file. The problem is due to the fact that by the time Apache reads the .htaccess file, it has already resolved all URLs to filenames. The standard solution to this is to supply a RewriteBase rule in your .htaccess. Try:

RewriteBase /

Above your RewriteRule for starters.

More documentation is here

dasil003
Using only this rule and RewriteEngine on in .htaccess, I still get redirected to website.com/home/user/www/abc. Adding in RewriteBase / gives the same result
admin
you mention `.htaccess`. Can you adjust `httpd.conf` instead? Are you on a hosting environment where they've simply disallowed this?
Abel
A: 

You could try it with the additional L flag to avoid any further rules to be applied:

RewriteRule ^([^/\.]+)/$ /$1 [R,L]

But if nothing else works, try this:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^/.?\ ]+)/[? ]
RewriteRule ^[^/.]+/$ %1 [R]
Gumbo