【问题标题】:Mod_rewrite - Create Clean URLsMod_rewrite - 创建干净的 URL
【发布时间】:2013-04-12 03:07:22
【问题描述】:

我们想为我们的网站创建干净的 URL。我已经搜索了如何做到这一点,并且我找到了一个代码 sn-p,但是它不符合我的要求。在这里,我将解释我们基本上想要什么。

这是基本网站的 URL:http://www.test.com

我们有一个“页面”菜单,其中列出了用户可以看到的特定页面。如下所示:http://www.test.com/pages.php

我们基本上希望当有人访问这个 /pages.php 时,它应该将他们重定向到 /pages(或完整的 URL:test.com/pages ) 或不重定向,只显示未找到的页面。

它还应该适用于以下任何可能的配置:

  • /页
  • /page/
  • /page.php

我查看了 Stackowerflow 的系统,它的工作方式符合我们的实际需要。所以每次我都在看:

  • stackoverflow.com/questions
  • stackoverflow.com/questions/
  • stackoverflow.com/questions.php - 显示页面未找到站点

这基本上是我需要的。我已经尝试过这段代码,但它不能完全满足我的需要:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

如果有人能清楚地解释如何实现这一点,我会很高兴。另外,如果您发现此帖子重复,请将 EXACT 帖子链接给我,因为我一直在寻找这个确切的问题,但没有找到任何答案。

谢谢

【问题讨论】:

标签: php apache mod-rewrite


【解决方案1】:

下面,第一条规则将 301 重定向 *.php 到没有 .php 的 URL,第二条规则将通过添加“.php”来处理所有其他 url:

    RewriteCond %{REQUEST_URI} ^/(.*?)\.php$
    RewriteRule ^/.*$ /%1 [R=301,QSA,L]

    RewriteRule ^/(.*)$ /$1.php

例如,

/page.php 将重定向到/page 并将处理page.php

【讨论】:

  • 它实际上并没有对页面做任何事情,即使我访问:/page.php or /page or /page/
  • 我应该只使用这些吗?没有别的吗?
  • 我会删除其他规则,至少看看它是否有效。但是我的规则会导致问题,例如静态文件(例如 *.jpg),因此您可能需要一些额外的条件。
【解决方案2】:

好的,写这样的。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName deli.localhost

    DocumentRoot /var/www/deli/
    <Directory /var/www/deli/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

                RewriteEngine on

                ## Externally redirect clients directly requesting .(php|aspx|html|htm|pl|cgi|asp)+ page 
                ## URIs to extensionless URIs
                ## If client request header contains php,aspx,html or htm file extension, redirect
                ## It avoids an infinite loop since "THE_REQUEST" is what the UA asks for, not what the 
                ## server translates it to on the second pass.
                ## THE_REQUEST syntax is something like: GET /page.html HTTP/1.0
                RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?\ HTTP [NC]
                RewriteRule ^([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?$ http://xyz.com/$1$3 [NC,R=301,L]


                FileETag none





                ErrorDocument 401 /401.php
                ErrorDocument 403 /403.php
                ErrorDocument 404 /404.php
                ErrorDocument 500 /500.php
                ErrorDocument 503 /503.php
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
</VirtualHost>

【讨论】:

    【解决方案3】:

    在另一个帖子中发现了这个。它显然从 url 中去除了扩展名 .php、.html、.cgi。

    RewriteCond %{REQUEST_URI} (.*)/$
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule (.*)/$ $1.html [L]
    
    RewriteCond %{REQUEST_URI} (.*)/$
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule (.*)/$ $1.php [L]
    
    RewriteCond %{REQUEST_URI} (.*)/$
    RewriteCond %{REQUEST_FILENAME}\.cgi -f
    RewriteRule (.*)/$ $1.cgi [L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f [OR]
    RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
    RewriteCond %{REQUEST_FILENAME}\.cgi -f
    RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
    

    【讨论】:

    • 这是从文件中清除 URL 扩展名,而不是真正让它表现得像来自数据库(通常的模式)的 SEO url(又名 slug)。
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2012-12-24
    • 2014-02-09
    • 2012-04-27
    • 2011-10-24
    • 2012-08-26
    • 2013-09-22
    相关资源
    最近更新 更多