duanji8615 2012-01-06 15:46
浏览 59

Apache mod_rewrite不起作用

I am trying to redirect requests to http://www.site.com/customer to https://www.site.com/customer and hide .php extensions, but it's not working. These are my configuration files:

httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

mysite.conf:

<Directory /var/www/site/customer>
  Order Deny,Allow
  Allow from all
</Directory>
Alias /customer /var/www/site/customer

.htaccess in /var/www/site/customer:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Am I missing something?

  • 写回答

1条回答 默认 最新

  • drw85135 2012-01-06 16:03
    关注

    Yep you are missing something ;)

    Here are some modifications:

    • you forgot the redirect and last tag in HTTPS rule
    • you forgot the QSA directive to keep the query string when there's a redirect

    So here it is, this might help, but maybe won't work 100% (but it's far still better than it was anyway):

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://$1 [QSA,NC,L]
    
    #unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1 [QSA,R=301,L]
    
    #redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.*)\.php$ $1 [QSA,R=301,L]
    
    #resolve .php file for extensionless php urls
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [QSA,L]
    

    Two hints:

    If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/mywebsite.rewrite.log"
    RewriteLogLevel 9
    RewriteEngine On
    

    My favorite tool to check for regexp:

    http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line