【问题标题】:Rewrite rule to remove ".php" extension and add trailing slash? [duplicate]重写规则以删除“.php”扩展名并添加斜杠? [复制]
【发布时间】:2011-12-09 22:14:50
【问题描述】:

可能重复:
How to remove .php extension and add slash on the url?

我需要在我的 .htacces(现在是空白的)中添加一些东西以从我的所有 url 中删除 .php 并强制使用斜杠。


我知道这种问题(通常更具体)已经被问过很多次了,但我发誓我找不到适合我的答案。

奇怪的是,我曾经有一个 .htaccess 代码来做我想做的事,我发誓我从这里得到了代码......但我把它弄丢了。

【问题讨论】:

标签: apache .htaccess url-rewriting


【解决方案1】:

尝试将以下内容添加到域根目录中的 .htaccess 文件中

RewriteEngine On
RewriteBase /

#skip existing files and directories
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f 
#capture every url path request ending with a slash 
RewriteCond %{REQUEST_URI} /(.+)/$ [NC]
#and rewritten to .php with that name. Assumes all .php files are in the root
RewriteRule . %1.php [NC,L]    


#add a trailing slash to all urls without one
RewriteCond %{REQUEST_URI} ^(/.+[^/]) [NC]
RewriteRule .  %1/  [L,R=301]

#remove .php from all urls and also add a trailing slash
RewriteRule (.*)\.php  /$1/  [L,R=301,NC]

编辑: 上面修改为将带有斜杠后缀的路径的请求重写为同名的 a.php。请注意,这假定所有 .php 文件都在根目录中

【讨论】:

  • 这样好吗? -因为我的htaccess是这样写的,IE,FF;他们甚至不会去我的页面。
  • 好吧,我写了:RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^(/.+[^/]) [NC] RewriteRule (.*)\.php /$1/ [L,R = 301,NC] - 它有效(删除.php 并添加斜杠),但我得到 404。我不想要那个。我该怎么办?
  • stackoverflow.com/questions/1068595/… 就是答案所在。还是谢谢
猜你喜欢
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 2013-03-12
  • 2017-07-14
  • 1970-01-01
相关资源
最近更新 更多