Htaccess redirect rule adds an unwanted variable
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, mod-rewrite, mod-alias, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :I'm trying to redirect logged-in users that land on a page to another page, but the redirect keeps adding the logged-in user's name to the path / URL.
Say I'm logged in as Mario. The redirect includes that username into the redirected path, throwing up a 404 error.
Below is the content of the .htaccess file (without the redirect):
Options +FollowSymLinks
RewriteEngine On
AddEncoding gzip .gz
AddEncoding gzip .gzip
<FilesMatch ".(js.gz|js.gzip)$">
ForceType text/javascript
</FilesMatch>
<FilesMatch ".(css.gz|css.gzip)$">
ForceType text/css
</FilesMatch>
RewriteCond %REQUEST_URI !^/index.php
RewriteCond %REQUEST_URI !/ow_updates/index.php
RewriteCond %REQUEST_URI !/ow_updates/
RewriteCond %REQUEST_URI !/ow_cron/run.php
RewriteCond %REQUEST_URI !/e500.php
RewriteCond %REQUEST_URI !/captcha.php
#RewriteCond %REQUEST_URI (/|.php|.html|.htm|.xml|.feed|robots.txt|.raw|/[^.]*)$ [NC]
RewriteCond %REQUEST_FILENAME (/|.php|.htm|.feed|robots.txt|sitemap.xml|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
This is my redirect:
Redirect /photo/useralbums /my-profile
The result is that the user ends up at: /my-profile/Mario
Redirect /photo/useralbums /my-profileThe result is that the user ends up at:
/my-profile/Mario
This will happen if the request is for /photo/useralbums/Mario since the mod_alias Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL.
If you need to redirect all URLs of the form /photo/useralbums/<username> to /my-profile then you will either need to use the mod_alias RedirectMatch directive, which matches using a regex and is not prefix-matching, or use a mod_rewrite RewriteRule directive. Since you are already using mod_rewrite for an internal rewrite you should use the mod_rewrite RewriteRule directive for the redirect as well in order to avoid potential future conflicts. (Different Apache modules run independently and at different times during the request. mod_rewrite always runs before mod_alias despite the apparent order of directives in your .htaccess file.)
Redirects should generally go before internal rewrites, so try the following instead before your existing mod_rewrite directives:
RewriteRule ^photo/useralbums(/[w-]*)?$ /my-profile [R,L]
Note there is no slash prefix on the RewriteRule pattern.
The above will redirect /photo/useralbums, /photo/useralbums/ or /photo/useralbums/<username> (but not /photo/useralbums/<username>/something) to /my-profile. Where <username> can consist of the characters a-z, A-Z, 0-9, _ or -.
However, this doesn't determine whether the user is "logged in" or not, as suggested in your question, since the redirect is unconditional.
Comments
Post a Comment