【问题标题】:Rewrite rule to remove .php extension using .htaccess使用 .htaccess 重写规则以删除 .php 扩展名
【发布时间】:2017-01-07 08:38:45
【问题描述】:

我在 StackOverflow 上找到了许多解决这个常见问题的解决方案,但似乎没有一个适用于我的设置。当我链接到一个 php 页面时,我仍然看到 .php 扩展名并希望拥有

index.php updated to 'website'

about-us.php to be website/about

我正在使用文件夹“网站”内的 wamp / localhost 开发网站。我的 .htacces 文件位于“网站”文件夹的根目录中。

我试图用来从 index.php 和其他 php 页面中删除 .php 扩展名的重写条件/规则是这样的。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

我的链接有这样的 URL

<a href="index.php">
<a href="about-us.php">

这是完整的 .htaccess 文件

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_deflate.c>
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

【问题讨论】:

  • 这里你可以直接链接到index而不是index.php&lt;a href="index"&gt;使用这个。
  • @阿布舍克。谢谢。是的,我们知道那个用例,但很好奇如何修复所有带有 .php 扩展名的页面。

标签: .htaccess mod-rewrite localhost


【解决方案1】:

它实际上并没有从 URL 中“删除”.php,它起到了重定向的作用。 换句话说,如果您不希望其他人在您的 URL 中看到 .php,那么您必须修复所有链接。

&lt;a href='www.example.com'&gt;

&lt;a href='www.example.com/about-us'&gt;

&lt;a href='www.example.com/blog'&gt;

您必须修复脚本中的所有链接。

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2014-12-22
    • 2015-03-14
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多