2

i have a project done ready..Now the client want to rewrite the entire url...ie(www.mydomain/page.php) to www.mydomain/page ect...

There are multiple pages.say about 10-15 in the folder..is there any possibility that we can rewrite the entire url of these pages at a given shot using .htaccess?

Also the links to these pages carry ".php" and ".html" extensions in order to navigate to another pages.. Now should I erase all these extensions manually or can i change it through other means(eg;.htaccess) Thanks

codelover
  • 317
  • 1
  • 11

3 Answers3

3

you would need to add something to know if it's PHP or HTML, like

all .php files becomes www.mydomain.com/p/page
and .html files become www.mydomain.com/h/page

(this would need another set of htaccess rules than the one below)

or you can change all .html files to .php so it would be easier..
you can now have all files hide their .php extension like

www.mydomain.com/page.php becomes www.mydomain.com/page

to do this, on your .htaccess put:

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

I've edited my answer after some researching because you needed to add 301 redirects from the old links back to the new ones so the code above should work now. credits to the answer here: Redirect .php urls to urls without extension

Community
  • 1
  • 1
reikyoushin
  • 1,993
  • 2
  • 24
  • 40
  • ,Yes the code is working fine provided i erase .php extension(converted all pages to php) manually... but now how about the links in these pages?should i change all the links manually by deleting .php in all the files?Is this the only case of dealing with .htaccess for multiple pages? – codelover Jul 30 '13 at 08:31
  • you don't need to erase the .php on the files.. if you access `www.mydomain.com/page` on the browser, you will get the contents of `www.mydomain.com/page.php`... – reikyoushin Jul 30 '13 at 13:13
1

You can use something like this in the .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

By this you can use urls like:

mydomain.com/page

or

mydomain.com/page/test

But notice all request will be moved to the index.php so index.php will be like a router.

Perry
  • 11,172
  • 2
  • 27
  • 37
1

Try this Apache provided feature in your root .htaccess:

Options +MultiViews
anubhava
  • 761,203
  • 64
  • 569
  • 643