如何在我的htaccess中modrewrite?

时间:2014-08-24 14:47:27

标签: regex apache .htaccess mod-rewrite

为什么我的.htacces不起作用?我有页面:

  

site.com/page.php

我希望看到这样的页面:

  

site.com/page /

htacces here:

RewriteEngine On
RewriteBase /
RewriteRule /(.*).php /$1

此外,我想看到从page.php重定向到/ page / too

如何?

1 个答案:

答案 0 :(得分:1)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]