Modify url with .htaccess file

自闭症网瘾萝莉.ら 提交于 2019-12-13 07:37:24

问题


I would like to delete extensions like .php, .html, etc from my url and this is what I use:

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

It works but I don't know if this is the best way to do this and I also don't understand how it works, could someone explain it?

The second thing I would like to realise is to make the URL more beautiful by doing something like this:

this is my URL: http://domain.com/portfolio/project.php?id=1 this is what I would like to see: http://domain.com/portfolio/project/1

Thanks in advance


回答1:


You can use these rules in site root .htaccess:

RewriteEngine On

# rewrite /portfolio/project/25 to /portfolio/project.php?id=25
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(portfolio/[\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA,NC]

# rewrite /portfolio/project to /portfolio/project.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# rewrite /portfolio/file to /portfolio/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]


来源:https://stackoverflow.com/questions/39408913/modify-url-with-htaccess-file

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