0

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

1 Answer 1

1

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
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

that did work for me @Stoyno. I edited muy question. Do I have to use absolute urls in my "Header" redirects?
@Richard If you want to redirect you should first check whether mod_rewrite is activated and then you must use absolute urls like this: header('Location: http://www.example.com/');
thanks, yes I figured it out and I will put a check in place in case of distributing the application. I was going to google if there was a disadvantage in using absolute urls. The only thing I could remember myself is that it wil cost in extra DNS lookups. Anyway, I will mark your answer.

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.