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.

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

报告相同问题?

悬赏问题

  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源