Omkar Omkar - 3 months ago 7
PHP Question

htaccess - redirecting to another page

I'm having problem with my htaccess file. Its redirecting page to root that is index.php



For example:
If I use url like http://domain.com/jobs or domain.com/jobs its redirecting page to www.domain.com/index.php

But if I use www.domain.com/jobs it is not redirecting page to other page.
Following is the htaccess code used by me

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</IfModule>

Answer

The [L] flag is a shortcut for Last, which tells mod_rewrite to stop processing rules after this one is matched.

RewriteRule ^ index.php [L] rule is processed and it redirects your request to the index.php, ommiting rules after this one.

The solution is - change the order of your rules. It should look like this:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>

        RewriteEngine On


    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]

        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
</IfModule>

It will be redirected to the www.domain.com, and then the other rules checks will be performed.

Comments