URL Rewriting For Apache

I have a better solution. I’ve decided to go a bit in details for those who need it. Take a look and read on.

 # Apache Rewrite Rules

 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules

Things I guess you want to know:

1. This is for sites that don’t use a front-end-controller where all requests are forced through index.php. If you need that, you must replace the third block with your own.

2. The example is showed with .php only. If you want to use it with .html for example, you must copy/paste the third block and replace “.php” on line (1,) 3 and 4 with “.html” (unless someone has a neat way of doing it?)

3. The content of “http://example.com/something/” could be served from either “http://example.com/something.php” or “http://example.com/something/index.php”.

4. To link to other pages, just href=”/some/thing/” or href=”http://example.com/some/thing/”.

5. With .css and .js you want to href=”/style.css” or href=”/folder1/style.css”. Else if you href=”style.css” from “http://example.com/folder2/file.php” it’ll look at “http://example.com/folder2/style.css”.

Method 2

RewriteEngine On

RewriteRule ^/?([a-z]+)/?$ $1.php [L]

Method 3

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.*)$ $1.php [NC,L]