dongyan1899 2013-01-25 03:36 采纳率: 100%
浏览 227
已采纳

无法获取mod_rewrite IfModule在.htaccess中工作

I'm trying to get my .htaccess file to forward all URLs to a single page with url parameters so that I can handle the page retrievals that way. What I want to happen is this: say the user types in http://mysite.com/users/dan it should forward to the page http://mysite.com/index.php?url=/users/dan.

Similarly, if the user accessed the URL http://mysite.com/random-link it should forward to http://mysite.com/index.php?url=random-link

Here is the code I tried in my .htaccess file, but it just keeps throwing a 500 error at me:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond % (REQUEST_FILENAME) !-f
RewriteRule (.*)$ index.php?url=$1 <QSA,L>
</IfModule>

I UPDATED MY CODE TO THIS AND IT STILL THROWS A 500 ERROR I changed the < and > to [ and ] and I removed the space after the % in the RewriteCond, but it still throws an error.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php?url=$1 [QSA,L]
</IfModule>

I'm a novice when it comes to .htaccess, so any help would be greatly appreciated, because I don't know what's causing the server to timeout.

  • 写回答

1条回答 默认 最新

  • duanbing6955 2013-01-25 03:39
    关注

    Couple of problems

    1. Rewrite flags go in square-brackets, eg

      [QSA,L]
      
    2. Your RewriteCondition syntax looks incorrect. Try

      RewriteCond %{REQUEST_FILENAME} !-f
      
    3. Just to be on the safe side, anchor your expression to the start of the string

      RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
      

    Lastly, RewriteEngine and subsequent modifiers requires the FileInfo override. Make sure your server config or virtual host <Directory> section for your document root has

    AllowOverride FileInfo
    

    Update

    Here's a typical rewrite scheme from an MVC project. This will ignore real files, directories and symlinks

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline