【问题标题】:How to add .php extension using .htaccess url rewriting?如何使用 .htaccess url 重写添加 .php 扩展名?
【发布时间】:2012-02-06 15:10:52
【问题描述】:

我在我的网站上设置了 .htaccess 文件,该文件将 SLUG 文本显示为 URL 中的页面名称,但没有扩展名。 例如真正的网址是

www.mywebsite.com/page.php?page=about-us

然后通过url重写加载到下面的url

www.mywebsite.com/about-us

下面是我的 .htaccess 文件代码

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^.*(images/|\.js|\.css).*$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ /page.php?page=$1

现在我想在所有页面上显示 .php 扩展名,这样 url 应该如下所示

www.mywebsite.com/about-us.php

我该怎么做?

【问题讨论】:

  • 您为什么想要 .php 扩展名。没有它,网址看起来更干净。这对 SEO 也更好。
  • 你说得对,但我的 seo 不理解它。他想要带有 .php 扩展名的页面:P
  • 告诉他这会损害他的网页在搜索引擎中的排名。只是一点点,但至少你没有对那个男人撒谎。

标签: php .htaccess url-rewriting


【解决方案1】:
Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^.*(images/|\.js|\.css).*$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)\.php$ /page.php?page=$1

【讨论】:

  • 在我的测试服务器上工作正常。请重新检查代码,因为 David 对原始代码所做的更改不会导致 500 错误。
【解决方案2】:
RewriteEngine On

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

php_flag display_errors on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2013-08-13
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多