Richard
Richard

Reputation: 4546

remove .php from url with htaccess

I need to know how to remove the ".php" from the directory index file

I don't want to see this:

http://www.domain.nl/wa.admin/index.php?login_msg=nopassword

I saw a lot of posts but they did not work for me for some reason.

these are my basic rules

RewriteRule ^admin[/]*$ /wa.admin [L]

RewriteRule ^([^./]{2}[^.]*)$ /index.php?page=$1 [QSA,L]

What do I have to do internally, because it comes from a redirect ? Do I have to use absolute url's to make it work?

thanks, Rich

Upvotes: 0

Views: 256

Answers (1)

Max
Max

Reputation: 1002

This should work for you.

RewriteBase /

# Stop rewrite process if the path points to a static file anyway
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L]

RewriteRule (.*) index.php

Explained in words: If the requested resource is a folder or a file don't rewrite, if not rewrite to the index.php.

In your case wa.admin/index.php?login_msg=nopassword will look like wa.admin/?login_msg=nopassword.

Don't forget to update you application. This rule only mapps requests to the appropriate files. This doesn't impact you HTML output. This means with this rewriting you can access both URLs but it's up to you which you want to link in your application.

If you want to distribute your app with others you may use the environment in combination with the if tag to determine whether mod_rewrite is available or not.

<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On

# Set flag so we know URL rewriting is available
SetEnv REWRITEURLS 1

# You will have to change the path in the following option if you
# experience problems while your installation is located in a subdirectory
# of the website root.
RewriteBase /

# Stop rewrite process if the path points to a static file anyway
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L]

RewriteRule (.*) index.php

</IfModule>

Use apache_getenv("REWRITEURLS"); to acess the value of the set variable.

You code might look like this (untested):

<?php
$rewriteurls = apache_getenv("REWRITEURLS");

if ($rewriteurls == '1') {
    //adjust all your links
}
?>

Upvotes: 1

Related Questions