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.

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集