【问题标题】:Remove .php from url WITH exceptions (htaccess)从带有异常的 url 中删除 .php (htaccess)
【发布时间】:2016-11-18 06:53:10
【问题描述】:

我有以下 .htaccess 文件:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1 

这基本上是为我的用户个人资料服务的,所以就像 twitter 有人可以输入:url.com/theirusername

但是,我想让某些页面(例如 /home)不指向个人资料,而是指向不同的 .php 页面,最后再次没有 .php。

谁能想到办法做到这一点?

谢谢!

【问题讨论】:

    标签: php mysql .htaccess url url-redirection


    【解决方案1】:

    您可以在此规则之前添加另一个规则.php

    RewriteEngine On
    
    # To internally redirect file to file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    # handle profile URLs    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\w-]+)/?$ account.php?username=$1 [L,QSA]
    

    【讨论】:

      【解决方案2】:

      只需添加更多规则以匹配所有其他页面,将其放在用户名规则之前。

      RewriteEngine On
      RewriteRule ^home$ home.php
      RewriteRule ^contact$ contact.php
      RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1
      

      【讨论】:

      • 感谢您花时间回答。我最后使用了第一个答案。
      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 2017-08-11
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多