Sherwin Flight
Sherwin Flight

Reputation: 2363

htaccess rewrite to force index.php

I have links that are in this format: http://www.EXAMPLE.com/index.php?page=pageid They are also available at: http://www.EXAMPLE.com/?page=pageid

How can I redirect the ones without the index.php filename to use the index.php part of the url?

I have other re-direct rules in my htaccess file, and they are working fine, I just don't know how to force the index.php part and include the query string as well.

Upvotes: 1

Views: 8573

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9539

Put the following rules in your .htaccess file in the root of your site

RewriteEngine On
RewriteBase /

#only for the root directory
RewriteCond %{REQUEST_URI} ^/$
#if the uri is not already index.php
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule ^$ /index.php    [R=301,L]

If there are still conflicts with your other rules, you can further limit this rule to just requests with a page= querystring param by adding the followind condition before the rewrite rule above

# to limit further to only the requests with a page= param
RewriteCond %{QUERY_STRING} ^([^&]+&)*page=.*$ [NC]

Upvotes: 4

Related Questions