1

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

2
  • How are you determining that a user is "logged in"?
    – MrWhite
    Apr 26, 2021 at 9:37
  • Because when I test it, the logged-in user's name gets included in the path. Your solution below did the trick, thanks a lot!
    – Gottano
    Apr 26, 2021 at 17:12

1 Answer 1

0
Redirect /photo/useralbums /my-profile

The 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.