的.htaccess mod_rewrite的子目录URL参数子目录、参数、htaccess、mod_rewrite

2023-09-02 00:33:33 作者:风逝无迹

我希望这不是问了一遍又一遍过,但使用谷歌,W3Schools的等我没有得到进一步的答复。因此,这里是我的问题: 我写了创建那种,我有我的主页项目的索引,使一个不错的列表基于一个信息文件,图像和文字传情的脚本。我主要是有我的项目在GitHub上的自述是在降价,所以我想我可以使用PHP动态生成从github上对需求的最新blob的降价的HTML,这样它就会自动更新。 我的目录结构是这样的:

I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question: I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically. My directory structure looks like this:

projects
    project1
        .remoteindex
        .info
    project2
        .remoteindex
        .info
    index.php
    .htaccess

所以,当刚 domain.tld /项目/ 要求我从的信息。信息,并作出所有项目的很好的指标。但是,如果 domain.tld /项目/ PROJECT1 / 的要求,我想在内部重定向到 domain.tld /项目/?DIR = PROJECT1 做我降价解析等。但 domain.tld /项目/ PROJECT1 / image.png 不应该被重定向。

So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.

这是我的尝试:

.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]

我把它重定向,而不​​是重写,这样我可以看到的错误是什么,因为我刚刚得到一个404,我重定向的URL是 domain.tld /家庭/网络/ web310 / HTML /项目/ index.php的?DIR = PROJECT1 所以显然是有出乱子与Web服务器的内部结构的路径是什么。 我希望你能理解我的问题是,我会很高兴,如果有人可以帮助我,因为我完全失去了的.htaccess 反正。

I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever. I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.

见我的回答如下的使用的.htaccess

See my answer below for the used .htaccess.

但奇怪的是,如果我有一个 index.html的中的子目录,我的本地Web服务器(Apache与XAMPP的Mac OS X 1.7.3)不重写和 index.html的获取显示,无一它的工作原理correctly.But,供应我的主页是重写使用和不使用 index.html的(这是我想要的)。上任何提示?

The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?

推荐答案

感谢所有帮助迄今!你们只是真棒!

Thanks for all the help so far! You guys are just awesome!

我想通了,无论您的解决方案中的一种共生关系很适合我:

I figured out that a symbiosis of both of your solutions works well for me:

RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]

当然只是没有 [R] ,这是我的错。 (见我的问题编辑的另一个问题请)。

Of course only without [R], this was my fault. (See my question edit for another question please).