I want to replace my.example.com/newsletter.php or example.com/newsletter.php to be example.com/newsletter/
I tried this:
RewriteEngine On
RewriteRule ^([a-z]+)$ /$1.php [L]
and what if I want http://example.com/communities/index.php?sub_id=1
to be a specific name like http://example.com/communities/Bridlewood/
possible?
The other answers are right about the leading slash, and you should catch the possibility of a trailing slash (bearing in mind that '$' marks the end of a string .
Try something like this:
RewriteBase / RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([a-z-]+)/?$ /$1.php [QSA,NC,L]
Note: The QSA
flag stands for 'query string append' and will pass any url query on to the relevant page.
hth
EDIT: For URL's that contain characters other than lower case alpha's you should change the range group, use a case-insensitive switch or simply use (.*)
as per other peoples examples!
You'll need to check that what was requested doesn't actually exist otherwise you'll be in all kinds of trouble! Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php
As for performing the second thing you ask, you'll obviously need to have a lookup function in your PHP code to get the id of the item you're after. Make sure you don't actually have a /communities
directory first.
There's plenty of help in the docs: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Please see my answer here: Rewrite all virtual subdomains to same folder using htaccess Pretty much same as what you are trying to do here.