【问题标题】:Rewrite dynamic URL's and delete ".php" extension重写动态 URL 并删除“.php”扩展名
【发布时间】:2014-01-25 14:00:50
【问题描述】:

我的 .htaccess 文件有问题。好吧,我正在尝试做两件事;删除所有 URL 的扩展名“.php”,例如:“localhost/about.php”到“localhost/about”或“localhost/about/”。并重写一个动态url:“localhost/user/index.php?usr=username”为“localhost/user/username/”或“localhost/username”。

我找到了一种方法来做这两件事。但是,如果我有删除“.php”扩展名的代码,那么重写动态 url 的代码就不起作用了。如果我没有删除“.php”扩展名的代码,那么重写动态 url 的代码就可以了。

这是我尝试打开个人资料页面时网站给我的错误:

找不到

在此服务器上找不到请求的 URL /user/enric。

找不到

在此服务器上找不到请求的 URL /user/enric.php。

我能做什么?这是.htaccess文件的代码:

<IfModule mod_rewrite.c>

Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]

# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

# User profiles
RewriteRule ^user/([^/]*)/$ /user/index/?usr=$1 [L,R=301]

</IfModule>

解决方案,作者 JON LIN

<IfModule mod_rewrite.c>

Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]

# User profiles
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L]

# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

</IfModule>

【问题讨论】:

    标签: php .htaccess mod-rewrite dynamic url-rewriting


    【解决方案1】:

    您需要添加一个检查,当您向请求添加 .php 扩展时,php 文件确实存在,并且您应该移动您的用户在 之前 重写您的 php 扩展内容:

    <IfModule mod_rewrite.c>
    
    Options -Multiviews
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
    RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
    RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]
    
    # User profiles
    RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L]
    
    # Delete ".php" extension and adds "/"
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
    RewriteRule (.*)\.php$ /$1/ [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.+)/$
    RewriteCond %{DOCUMENT_ROOT}%1.php -f
    RewriteRule (.*)/$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule .*[^/]$ $0/ [L,R=301]
    
    </IfModule>
    

    【讨论】:

      【解决方案2】:

      您可以通过在线工具生成 htaccess 规则,例如 http://www.generateit.net/mod-rewrite/index.php

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 2014-12-22
        • 1970-01-01
        • 2022-03-04
        • 1970-01-01
        • 2019-04-21
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        相关资源
        最近更新 更多