Htaccess Remove part of URL

▼魔方 西西 提交于 2019-12-20 06:35:22

问题


I have this URL: http://www.website.nl/pagina/4/wie-ben-ik.html

"pagina" is a real php document. "4" is the id of the webpage, with the id I get the results from the database.

Can I change this URL to http://www.website.nl/wie-ben-ik with htaccess?

Thank you!


回答1:


You have to pass the id some way or the other if you want it to query from the database.

Either use a query string (name-value pair) or have it in the URI like your are doing above.

If you do not want pagina in your URI you can have your URI's like this: http://www.website.nl/wie-ben-ik/<the number you want to send>

RewriteCond %{REQUEST_URI} ^/(wie-ben-ik)/(\d+)
RewriteRule ^ /pagina/%2/%1\.html



回答2:


Try adding the following to the .htaccess file in the root directory of your site.

RewriteEngine on
RewriteBase / 

#redirect http://www.website.nl/pagina/4/wie-ben-ik.html to http://www.website.nl/wie-ben-ik 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /pagina/4/(wie-ben-ik)\.html [NC]       
RewriteRule ^ %1 [L,R=301]

#process wie-ben-ik as http://www.website.nl/pagina/4/wie-ben-ik.html
RewriteRule ^([-a-zA-Z]+)$  pagina/4/$1.html [L,NC] 


来源:https://stackoverflow.com/questions/9132578/htaccess-remove-part-of-url

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