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.

Rahul Khosla
  • 349
  • 9
  • 21

4 Answers4

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

Rick Kuipers
  • 6,616
  • 2
  • 17
  • 37
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..

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]
Asko Foe
  • 51
  • 2
  • 8
0
RewriteRule ^/(home|shops) /$1.php

second only manual

RewriteRule ^/shops/shopname1/ /whateveryouneed.php
monkeyinsight
  • 4,719
  • 1
  • 20
  • 28