Removing .php extension from URLs using .htaccess? [duplicate]

扶醉桌前 提交于 2019-12-12 10:15:36

问题


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.


回答1:


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




回答2:


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..




回答3:


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]



回答4:


RewriteRule ^/(home|shops) /$1.php

second only manual

RewriteRule ^/shops/shopname1/ /whateveryouneed.php


来源:https://stackoverflow.com/questions/16003269/removing-php-extension-from-urls-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!