How to remove .php extension and add slash on the

2020-03-05 07:12发布

Here is my current .htaccess code

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

it is works only in removing .php extension

from http://localhost/mysite/news.php?category=cat1&id=1 to http://localhost/mysite/news/cat1/1/

and from

http://localhost/mysite/news.php?category=cat1&year=2011&month=10&day=25&id=1 to http://localhost/mysite/news/2011/10/25/1

How to write complete .htaccess for the clean url above?

3条回答
趁早两清
2楼-- · 2020-03-05 07:29

Try This

RewriteRule ^(.*)\/$ $1.php [NC]

for example

http://www.exapmle.com/contact-us/

查看更多
手持菜刀,她持情操
3楼-- · 2020-03-05 07:30

Add an optional / to your pattern:

RewriteEngine On

# rewrite news articles and pass news id as a GET parameter to news.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/news/(\d+)/?$ /mysite/news.php?newsid=$1 [NC,L,QSA]

# rewrite all other page requests to .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/(.*)/?$ /mysite/$1.php [NC,L,QSA]
查看更多
贼婆χ
4楼-- · 2020-03-05 07:31

You could just try a simple RewriteRule:

RewriteEngine on
RewriteRule ^/?(\w+)/?$ $1.php

This tacks on .php to any file-looking url: example.com/somethin becomes example.com/somethin.php, example.com/something/else/ becomes example.com/somethin/else.php, etc.

The only problem with this is if you try to access an actual folder, like example.com/images or something.

查看更多
登录 后发表回答