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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵