dtja73027 2013-04-28 13:54
浏览 49
已采纳

mod_rewrite / foo到/index.php?id=foo AND / foo / foo2到/index.php?id=foo/foo2

I have a problem with mod_rewrite.

I want to redirect those URIs in the title. I use the following rule

RewriteEngine on

RewriteCond $1 !^(folders not to be redirected e.g. css|images)
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php)

RewriteRule ^(.*)$ index.php?id=$1 [L]

It work only if I put all the resources in a folder, else it would tell me:

"/foo/index.php" not found.

So to solve that I put all the resources in the folder "www"

But when I try to load the resources from a subfolder of e.g. "foo" it tells me:

The requested URL "/foo/foo2" was not found on this server.

How can I load resources from a subfolder like "/foo/foo2" or even "/foo/foo2/foo3"?

And how can I solve the problem with the automatic search for index.php in a folder?

  • 写回答

1条回答 默认 最新

  • dsgnze6572 2013-04-28 15:44
    关注

    I believe you can use the following to achieve your desired result. It doesn't filter by file extension, but rather checks to see if the file actually exists. One thing that is a little different is that it first appends a trailing slash to your links, something you may not want.

    RewriteEngine on
    RewriteBase /
    
    # This appends a trailing slash. You will have to update http://so with your domain.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://so/$1/ [L,R=301]
    
    # This does the internal redirect
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)/$ /index.php?id=$1 [L]
    

    If you don't want the tailing slashes, you could use the following

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?id=$1 [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000