【问题标题】:adding .php extension if not exist如果不存在,则添加 .php 扩展名
【发布时间】:2021-07-19 00:20:40
【问题描述】:

我有下面的重写,但是如果用户最后用 .php 键入 URL,我想替换没有扩展名 .php 的 URL,我该如何修改下面的重写脚本?

# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]

【问题讨论】:

  • 你能举几个例子吗

标签: .htaccess url mod-rewrite url-rewriting friendly-url


【解决方案1】:

使用您展示的示例,请让您的 .htaccess 文件具有以下规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
# metchants/yyy/index.php to redirect /m/yyy rule
RewriteRule ^merchants/([\w-]+)/index\.php/?$ m/$1 [R=301,L,NC]
# merchants/yyy/abc.php to redirect /m/yyy/abc rule
RewriteRule ^merchants/([\w-]+)([\w-]+)\.php/?$ m/$1/$2 [R=301,L,NC]
# merchants/yyy/xyz/abc.php to redirect /m/yyy/xyz/abc rule
RewriteRule ^merchants/([\w-]+)/([\w-]+)/index\.php/?$ m/$1/$2 [R=301,L,NC]

# To handle /m/yyy/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [QSA,L,NC]

# To handle /m/yyy/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [QSA,L,NC]

# To handle /m/yyy/xyz/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [QSA,L,NC]

【讨论】:

  • @Kelvin,你能告诉我这是否对你有用吗?谢谢。
猜你喜欢
  • 2017-02-10
  • 1970-01-01
  • 2022-12-11
  • 2019-02-25
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多