codelover
codelover

Reputation: 317

.htaccess multiple url rewriting

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

Upvotes: 2

Views: 1091

Answers (3)

reikyoushin
reikyoushin

Reputation: 2021

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

Upvotes: 3

anubhava
anubhava

Reputation: 784898

Try this Apache provided feature in your root .htaccess:

Options +MultiViews

Upvotes: 1

Perry
Perry

Reputation: 11700

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.

Upvotes: 1

Related Questions