htaccess重写下划线,扩展名和尾随斜杠

时间:2015-04-13 07:37:10

标签: apache .htaccess rewrite

我已经设置了htaccess以删除php扩展并添加尾部斜杠。

现在我需要弄清楚如何在同一个htaccess文件中用斜杠替换下划线,例如:

  • 旧:http://localhost/website/parentpage_sub-page.php
  • 新:http://localhost/website/parentpage/sub-page/

有多智能方法可以为多个网址执行此操作吗?

这是当前的htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

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

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