How to remove .php extension and add slash in URL?

Here is my current .htaccess code

RewriteEngine On

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

only works when removing the .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 at http://localhost/mysite/news/2011/10/25/1

How to write full .htaccess for the clean URL above?

+3
source share
3 answers

try it

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

eg

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

+3
source

Add the / option to your template:

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]
+2
source

RewriteRule:

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

.php URL-, : example.com/somethin example.com/somethin.php, example.com/something/else/ example.com/somethin/else.php ..

- , , example.com/images - .

+1
source

Source: https://habr.com/ru/post/1789140/


All Articles