1) I would like to create a rewrite rule that removes the file at the end of the URL if the file extension is PHP or (S)HTM(L). For example:
This: http://www.example.com/index.php
Becomes: http://www.example.com/
This: http://www.example.com/index.php?action=login
Becomes: http://www.example.com/?action=login
2) I would like to set up some URL redirect with my .htaccess so that if someone browse a certain path is automatically redirected to /index.php. For example:
This: http://www.example.com/classes/(index.php)
Redirected To: http://www.example.com/(index.php)
This: http://www.example.com/classes/class.php
Redirected To: http://www.example.com/(index.php)
This: http://www.example.com/classes/style.css
Redirected To: http://www.example.com/(index.php)
Any clue about it?
Many thanks!
For 1) you may invert your question, the job of mod-rewrite is to read dirty url and find the real file, so what you want is to use a false url (without index.php) and that mod-rewrite finds the index.php. Mod-rewrite will not modify your url, it’s your application code job (PHP?).
So let’s say your application is writing this url:
The fact that apache will use
or
or
to respond is not handled by mod-rewrite but simply by the Apache configuration directive
For http://www.example.com/?action=login it should also work.
For 2): well it’s unclear. Do you always want to redirect people trying to access your classes directory? Then 2 things:
You do not need mod-rewrite to make simpel redirect, mod_alis, which is a module that you certainly have (99% of chances) procides the
RedirectandRedirectMatchkeywords so that you can write:RedirectMatch permanent /classes/.* http://www.example.com/
as always check this page: for rules that can be done easily without entering in the brain convolutions of mod-rewrite.