【问题标题】:.htaccess how can I implement both "vanity url" and "no extension".htaccess 如何同时实现“虚 url”和“无扩展名”
【发布时间】:2012-03-05 22:40:01
【问题描述】:

我正在使用来自 stackoverflow.com/q/8583856 的 htaccess -

RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

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

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/profile.php?u=$1 [NC]

Everything works great unless I type www.mysite.com into address bar -    
returns mysite.com/profile?u=index.html.var  
which errors "Unknown column index.html.var in where clause"  

有人知道如何将其转至 mysite.com/index 吗?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    将您的代码更改为此并告诉我它的行为方式(清除浏览器缓存后):

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [R=302,L]
    
    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ $1 [R=302,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ profile.php?u=$1 [NC,L]
    

    一旦您确定它工作正常,然后将 R=302 更改为 R=301

    【讨论】:

    • 谢谢,阿努巴瓦!效果很好。我希望我能在 17 小时前收到您发送的信息。 Stack 没有通知我。
    【解决方案2】:

    尝试添加条件以检查路径是否为空(即root)

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond $0 ^$
    RewriteRule .* - [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2012-12-21
      • 2012-04-19
      • 2016-07-10
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      相关资源
      最近更新 更多