删除 .htaccess 中的 GET 参数并获取 2 个 URL

Removing GET parameter in .htaccess and get 2 URLs

我有这种URL:

http://www.example.com/index.php?page=contest&contest=this-one-contest

我正在尝试在此处获取 2 个正常运行的 URL。

  1. 我想做的是得到这种 URL:

    http://www.example.com/contest

使用这个 htaccess 重写规则已经完成了

RewriteRule ^([a-z_-]+)/([a-zA-Z0-9_-]+)$ index.php?page= [L,QSA]
RewriteRule ^([a-z_-]+)$ index.php?page= [L,QSA]
RewriteRule ^([a-z_-]+)/$ index.php?page= [L,QSA]
  1. 我想做的是在第二个 URL:

    中得到这种 URL

    http://www.example.com/contest/this-one-contest

我不知道使用 htaccess 是否可行,我不想创建一个名为“contest”的新文件夹。

我已经试过了,但还是不行。

RewriteRule ^([a-z_-]+)contest/([a-zA-Z0-9_-]+)$ index.php?page=&contest= [L,QSA]
RewriteRule ^([a-z_-]+)contest/$ index.php?page= [L,QSA]
RewriteRule ^([a-z_-]+)contest/$ index.php?page= [L,QSA]

您不必使用两步法,您可以立即使用非常通用的方法重写它:

RewriteEngine on
RewriteRule ^/?([a-z_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?page=&contest= [L,QSA]
RewriteRule ^/?([a-z_-]+)/?$ /index.php?page= [L,QSA]

这当然假设没有其他潜在的 URLs 匹配您想要处理不同的相同模式......因此,如果您只希望在 URL 以 /content/ 开头,那么该变体应该做:

RewriteEngine on
RewriteRule ^/?contest/([a-zA-Z0-9_-]+)/?$ /index.php?page=contest&contest= [L,QSA]
RewriteRule ^/?contest/?$ /index.php?page=contest [L,QSA]

当然,如果第二个参数也是一个文字,您想要对其做出反应的固定参数,那么您可以获得更具体的信息:

RewriteEngine on
RewriteRule ^/?contest/this-one-contest/?$ /index.php?page=contest&contest=this-one-contest [L,QSA]
RewriteRule ^/?contest/?$ /index.php?page=contest [L,QSA]

我终于找到了适合我的解决方法,我对@arkascha 解决方案进行了很多自我调整,

RewriteEngine on 
RewriteRule ^/?([a-z_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?page=&contest= [L,QSA] 
RewriteRule ^([a-z_-]+)/$ index.php?page= [L,QSA] 
RewriteRule ^/?contest/?$ /index.php?contest=1$ [L,QSA]