php.htaccessdreamhost

Redirecting to wrong URL with Question Mark (Dreamhost)


Assume:

Capital = https://www.emeraldbabe.com/profile/Prabhas/347
Small = https://www.emeraldbabe.com/profile/prabhas/347
Wrong = https://www.emeraldbabe.com/profile/prabhas/347?/profile/Prabhas/347

I want to Redirect from Capital to Small

But when I am opening Capital it is redirecting to the Wrong URL.

Below is My Htaccess Code

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]


# Redirect URLs of the form "/index.php/<URL>" to "/<URL>"
# Also handles "/index.php" only
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index.php(?:/(.*))?$ /$1 [R=301,L]

RewriteEngine on
ErrorDocument 404 https://www.emeraldbabe.com/
ErrorDocument 402 https://www.emeraldbabe.com/
ErrorDocument 501 https://www.emeraldbabe.com/
ErrorDocument 502 https://www.emeraldbabe.com/
ErrorDocument 401 https://www.emeraldbabe.com/

Redirect 301 /profile/Prabhas/347 /profile/prabhas/347

Solution

  • Your mod_alias Redirect directive is conflicting with your front-controller (first rule that uses mod_rewrite). Redirect always works on the requested URL-path, but it is catching the query string from the rewritten URL.

    You have the rules in the wrong order and you need to use mod_rewrite (ie. RewriteRule) instead to avoid this conflict. Different Apache modules work independently and not necessarily in the order specified in the config file.

    Aside: Redirecting all your error responses to the homepage is generally bad for SEO and users.

    For example:

    ErrorDocument 404 https://www.example.com/
    ErrorDocument 402 https://www.example.com/
    ErrorDocument 501 https://www.example.com/
    ErrorDocument 502 https://www.example.com/
    ErrorDocument 401 https://www.example.com/
    
    RewriteEngine on
    
    # Correct case of URL
    RewriteRule ^profile/Prabhas/347$ /profile/prabhas/347 [R=301,L]
    
    # Redirect URLs of the form "/index.php/<URL>" to "/<URL>"
    # Also handles "/index.php" only
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index.php(?:/(.*))?$ /$1 [R=301,L]
    
    # Front-controller
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]