kapeels
kapeels

Reputation: 1702

mod_rewrite config for /

I have the following .htaccess config

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule \.git - [F,L]

RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]

RewriteRule ^users/([0-9]+) profile.php?id=$1 [L]

RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?u=$1 [L]

Now, whenever somebody visits the landing page, they get redirected using the last rule for profile.php?u=$1.

How do I change the configuration so that www.example.com and www.example.com/ are mapped to index.php and not profile.php?

Upvotes: 1

Views: 104

Answers (2)

Hitesh Joshi
Hitesh Joshi

Reputation: 724

I will suggest not to do it this way.

Instead, simply user this

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

This will send all your requests to index.php page, from there create a router.php and pass on the requests to that page, using php.

but in case you do, just add

 RewriteRule ^/?$ index.php [L]

Like Michael suggested.

Here is a simple tool to test your rules, if you wish to

Apache RewriteRule tester

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270677

Match the empty string or single slash just after the ^index rule:

RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
# Match root request with optional slash
RewriteRule ^/?$ index.php [L]

Upvotes: 1

Related Questions