1

Is it possible to remove the .php part from all of my URLs using a .htaccess file?

Two examples:

http://url.com/home.php http://url.com/shops.php 

Become:

http://url.com/home/ http://url.com/shops/ 

All the help would be massively appreciated.

16
  • 1
    What do you mean in the extra? Commented Apr 14, 2013 at 19:40
  • @Servant im wanting to merge 2 links together, im going to be having about 10 shops and it would look better if each shop was in a category and so I don't mind making a new directory with the shop names inside. Example: shops.php Shops-Folder>shopName1,shopName2 url.com/shops/shopname1/ url.com/shops/shopname2/ Commented Apr 14, 2013 at 19:45
  • What? Do you want to rewrite /$var1/$var2.php into /$var1/$var2/ ? Commented Apr 14, 2013 at 19:48
  • @Servant I think its just to hard to explain and probs impossible. Thanks for your time but i'l edit the extra out. Commented Apr 14, 2013 at 19:51
  • No, it's possible.. I'll try it, just wait for my answer.. Commented Apr 14, 2013 at 19:54

4 Answers 4

4

This should work:

RewriteEngine On RewriteRule \/([^\/]+)\/$ $1.php 

It will serve you the file named after the last segment of the url:

http://example.com/shops/shopname1/ -> shopname1.php

http://example.com/shops/ -> shops.php

Sign up to request clarification or add additional context in comments.

Comments

3

To rewrite /$var/ into /$var.php and then redirect /$var.php into /$var/ just use these directives in your .htaccess file:

# once per htaccess file Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z0-9-_]+)/? /$1.php RewriteRule ^([a-z0-9-_]+).php$ /$1/ [R] 

And if you also want to rewrite the specific URL /shops/shopName1/ into the specific URL /shopName1.php and then redirect /shopName1.php into /shops/shopName1/ then you should use this code below instead the code above:

# once per htaccess file Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z0-9-_]+)/?$ /$1.php RewriteCond %{REQUEST_URI} !^/shopName1.php$ RewriteRule ^([a-z0-9-_]+).php$ /$1/ [R] RewriteRule ^shops/shopName1/?$ /shopName1.php RewriteRule ^shopName1.php$ /shops/shopName1/ [R] 

But remember there's no variable on /shops/shopName1/, give it a try, if you want..


It seems there's a problem about redirection with DirectoryIndex home.php. But I think, this is the code that you want, but in this time, we excluded any redirection:

DirectoryIndex /home.php Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/shopName1/?$ RewriteCond %{REQUEST_URI} !^/shopName2/?$ RewriteCond %{REQUEST_URI} !^/shopName3/?$ RewriteCond %{REQUEST_URI} !^/shopName4/?$ RewriteCond %{REQUEST_URI} !^/shopName5/?$ RewriteRule ^([a-zA-Z0-9-_]+)/?$ /$1.php RewriteRule ^shops/shopName1/?$ /shopName1.php RewriteRule ^shops/shopName2/?$ /shopName2.php RewriteRule ^shops/shopName3/?$ /shopName3.php RewriteRule ^shops/shopName4/?$ /shopName4.php RewriteRule ^shops/shopName5/?$ /shopName5.php 

Just use your logic to add more conditions and rules..

Comments

1

I would suggest to simply stop using regular .php files for pages and instead of a framework or a request router, however if you really do want to do this, then in order to get shops.php to shops you need to create a .htaccess file and add the following to it:

RewriteRule (.*) $1.php [L] 

This will rename all of the something.php to something on the URL bar.

An example of a complete .htaccess file would be:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) $1.php [L] 

10 Comments

Do I also write RewriteEngine on before those lines?
Yes RewriteEngine On before the two lines.
I added an example of a complete .htaccess file.
@Askp Foe Ok this is my complete .htaccess file: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !\.php$ [NC] RewriteRule ^/?(.*)$ $1.php [L,R=301] </IfModule> DirectoryIndex home.php I keep getting this: http://rahulkhosla.co.uk/home/.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php
I just edited that 10 times, yet cant find out how <br/>'s work sorry for the inconvenience..
|
0
RewriteRule ^/(home|shops) /$1.php 

second only manual

RewriteRule ^/shops/shopname1/ /whateveryouneed.php 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.