0

There are a few things I want done to the URLs of my site that I cannot seem the .htaccess file to do.

1 remove file extension f.e. example.com/file.php should be example.com/file

2 remove the www. f.e. www.example.com should be example.com (I got this part to work, but I would hate it if after I put in fix and this no longer worked

3 no one should be able to see index.php at the end of root f.e. example.com/index.php should be example.com

4 my blog page should have nice urls f.e. example.com/blog.php?article=the-name-of-article should be example.com/blog/the-name-of-article

here is my current .htaccess file

rewrite URLs

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]

when I try to go to blog/the-name-of-article I get a internal server error.

kqlambert
  • 2,693
  • 6
  • 33
  • 50
  • 1 and 2 are more suited as duplicate of http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess/ and 4 as a duplicate of http://stackoverflow.com/questions/36815217/replace-url-query-string-with-slash-for-a-friendly-url (so question may be too broad after all). – Tunaki Aug 30 '16 at 21:06

2 Answers2

0

To remove .php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

see Remove .php extensions with .htaccess without breaking DirectoryIndex

EDIT For pretty URLs check this tutorial: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Community
  • 1
  • 1
Fallexe
  • 596
  • 2
  • 11
  • Well I removed my way of getting rid of extensions and added yours and now the entire site will not load – kqlambert May 31 '13 at 21:20
0

From the body and comments of your .htaccess it appears that I would have provided it in the past :P

Only thing wrong in your .htaccess is ordering of rules. Always have them from most specific to most generic. Have your code like this:

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^blog/(.*)$ /blog.php?article=$1 [QSA,L]

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
anubhava
  • 761,203
  • 64
  • 569
  • 643