1

I'm having the following issue: I have this url www.mysite.com/inde.php?SomePageName and I want to rewrite it to www.mysite.com/SomePageName also, I want to get rid of index.php when the url is called without any params eg: mysite.com/index.php or mysite.com/index.php? should redirect to mysite.com/

My htaccess file looks like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%2? [L,R=302]
RewriteRule ^index.php$ http://www.mysite.com/ [R=302,L]

Now it's doing a loop because of the last declaration.

Is there any way that I can achieve this ?

Thanks

PS: the whole htaccess

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^cumpara-(.*)-(.*)\.html$ index.php?Profile&ID=$2 [L]
RewriteRule ^lista-scut-motor-metalic-(.*)-(.*)-(.*)\.html$ index.php?List&SubID=$3&Categorie=$1&Subcategorie=$2 [L]
RewriteRule ^scut-motor-metalic-(.*)-(.*)\.html$ index.php?ViewCat&CatID=$2 [L]
RewriteRule ^foto-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=300&q=100 [L]
RewriteRule ^product-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=530&q=100 [L]
RewriteRule ^side-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=175&h=110&q=100 [L]
RewriteRule ^zoom-(.*)-(.*).jpg$ data/$1/$2.jpg [L]

RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^.*$ http://www.mysite.com/%1/? [R=302,L]

RewriteRule Despre index.php?Despre [L]
RewriteRule Help index.php?Help [L]
RewriteRule Livrare index.php?Livrare [L]
RewriteRule Contact index.php?Contact [L]

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

ErrorDocument 404 /404.html

1 Answer 1

1

If it was after me, the simplest solution was to handle the duplicate of index.php via PHP

Having this .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

You'll have all REQUEST_URI's written into PHP, so, check if the index.php is present and if it is, then redirect to a non-index.php link :D Like this:

if( strpos($_SERVER['REQUEST_URI'], "?") !== FALSE ) {
    $checkq = explode("?", $_SERVER['REQUEST_URI']);
    $vars = "?".$checkq[1];
    $checkq = $checkq[0]; 
}else { 
    $checkq = $_SERVER['REQUEST_URI'];
    $vars = ''; 
}
if( strpos($checkq, "/") !== FALSE ) {
    $link = explode("/", $checkq);

    if($link[1] == "index.php"){
        unset( $link[1] );
        $link = implode("/",$link);

        header('Location: http://'.$_SERVER['HTTP_HOST'].$link.$vars);
        exit();
        }
}

This should work even with you GET variables / your custom .htaccess.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.