首页 > 解决方案 > 如何编写干净的 url 我有两个来自不同文件夹的 url 第一个 url 来自根目录,另一个来自 root/tutorials/htaccess

问题描述

我有以下两个网址我想让它干净的网址

原始网址

https://www.evidhya.com/tutorials/get_data.php?qid=ADO

我想像这样清理网址

https://www.evidhya.com/tutorials/ADO

第二个网址是https://www.evidhya.com/tutorials/tutorials.php?qid=459

我想像这样清理网址https://www.evidhya.com/tutorials/459

我试过这些规则

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(\d+)/?$ tutorials.php?qid=$1 [L,QSA]

RewriteRule ^([\w.-]+)/?$ get_data.php?qid=$1 [L,QSA]

在上述规则的帮助下,我能够实现这样的干净网址

https://www.evidhya.com/tutorials/ADOhttps://www.evidhya.com/tutorials/459

标签: php.htaccess

解决方案


/tutorials/使用以下规则创建一个 .htaccess 内部目录:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(\d+)/?$ tutorials.php?qid=$1 [L,QSA]

RewriteRule ^([\w-]+)/?$ get_data.php?qid=$1 [L,QSA]

确保 中没有其他规则tutorials/.htaccess

现在在浏览器中加载这两个 URL 来测试:

  • https://www.evidhya.com/tutorials/ADO
  • https://www.evidhya.com/tutorials/459

推荐阅读