duanchen1937 2014-05-30 12:57 采纳率: 0%
浏览 47
已采纳

.htaccess在服务器上表现不同

So I have a website that successfully rewrites the url parameters to a clean url but on another server the same .htaccess does not work.

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
  RewriteRule .* – [F,L]

  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteCond %{SCRIPT_FILENAME} !-f

  RewriteRule ^project/(\d+)*$ ./project.php?project=$1
</IfModule>

What is not working is the last line there. I need something.com/project/star-trek to be the url the user gets when clicking a link, but they are served up something.com/project.php?project=star-trek while still seeing the clean url.

On Server1 this code above works perfectly but the same code on Server2 does not. RewriteRule does work on Server2 because we can use

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

This does work to remove the '.php' on Server2 as well as Server1.

I did check that .htaccess is being allowed in the httpd.conf and I have changed all the AllowOverwrite(s) to All

I'm at a loss here and would appreciate any help. Thank you

  • 写回答

1条回答 默认 最新

  • dongwu9972 2014-05-30 13:46
    关注
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{HTTPS} !=on
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    
        RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
        RewriteRule .* – [F,L]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^project/(.*?)/?$ ./project.php?project=$1 [L]
    </IfModule>
    

    That works on my machine, in your original script the bits I noticed were:

    • the RewriteEngine wasn't switched on (but it may have been in httpd.conf or httpd-vhosts.conf)
    • the RewriteRule was only matching digits and not allowing trailing slashes: ^project/(\d+)*$

    I've made the RewriteRule an ungreedy wildcard match that excludes trailing slashes; works in an .htaccess file under Apache 2.4.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?