【问题标题】:How to replace in .htaccess ? on / for get request如何在 .htaccess 中替换? on / for 获取请求
【发布时间】:2023-04-06 02:53:01
【问题描述】:

我有一个链接 - https://mysite.ru/blog/show?url=c_plus_plus_krut

我需要什么 - https://mysite.ru/blog/show/c_plus_plus_krut

从网上尝试了许多解决方案,但都失败了。最后一个是:

RewriteCond %{QUERY_STRING} ^url=([A-z0-9]+)(&|$) [NC]
RewriteRule ^show$ /%? [L,R=301,NC]

请帮帮我!

更新: 注意到如果你添加 .php 来显示,它可以工作。但我需要去掉文件扩展名:

https://mysite.ru/blog/show.php/c_plus_plus_krut

.ht访问代码:

RewriteEngine On

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

ErrorDocument 404 /404

【问题讨论】:

  • 您能否确认您在浏览器中点击了 url https://mysite.ru/blog/show/c_plus_plus_krut?您想在后端从https://mysite.ru/blog/show?url=c_plus_plus_krut 提供哪个服务?
  • @RavinderSingh13 在当前链接下一切都对我有用。但她长得并不漂亮。这将是一篇文章预览。如果我正确理解您的问题
  • 您的 htaccess 是否位于 blog 文件夹内?
  • @AmitVerma no,在网站的根目录

标签: .htaccess


【解决方案1】:

根据您上面的 cmets,您正在寻找可以在内部重写您的新 URL 的 RewriteRule

https://mysite.ru/blog/show/c_plus_plus_krut 给旧的https://mysite.ru/blog/show?url=c_plus_plus_krut

您可以为此使用以下规则:

RewriteEngine on

RewriteRule ^blog/show/(.+)/?$ /blog/show.php?url=$1 [L]

最后不使用 .php 来访问你的文件,你可以使用这个:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

这就是全部:

RewriteEngine on

RewriteRule ^blog/show/(.+)/?$ /blog/show.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多