【问题标题】:.htaccess rewrite url with page name slash GET parameter.htaccess 用页面名称斜杠 GET 参数重写 url
【发布时间】:2021-06-29 14:40:34
【问题描述】:

我正在尝试将我的 URL 从 request/2 重写为 request?number=2

我试着在我的.htaccess写这个

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/(.*)$ request?number=$1 [NC,L]

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

但结果是404 Error

有人可以帮我吗?

【问题讨论】:

  • 试试:RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]
  • 它有效,谢谢!有什么办法可以避免写扩展名吗? @anubhava
  • 为什么要避免?扩展名未显示在 URL 中,仅在规则中使用

标签: .htaccess mod-rewrite parameters url-rewriting get


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。请在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##To remove .php extension eg--> someone hits http://localhost:80/test.php it will redirect to http://localhost:80/test  
RewriteCond %{THE_REQUEST} \s([^.]*)\.php\s [NC]
RewriteRule ^ %1 [R=301]
 #RewriteRule ^(.*)$ $1.php [L]

##All non-existing files/directories will come in this rule whose URI starts from request. 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]

##All non-existing files/directories will come in this rule set apart from URI which starts from request which is taken care above.   
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

【讨论】:

  • 有一些错误,它返回一个错误500
  • @IvanCisternino,好的,请告诉我您正在点击的网址(示例网址将在此处有所帮助)。
  • 错误在于移除扩展,在前5行@RavinderSingh13
  • 现在没有更多错误了,但是重定向到没有扩展名的页面不起作用@RavinderSingh13
  • @IvanCisternino 这似乎是浏览器缓存问题。尝试使用其他浏览器测试 URL
猜你喜欢
  • 1970-01-01
  • 2014-11-22
  • 2015-03-19
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
相关资源
最近更新 更多