未知目录的RewriteRule

编程入门 行业动态 更新时间:2024-10-25 18:23:10
本文介绍了未知目录的RewriteRule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我试图获得一个mod_rewrite规则,以将请求重定向到带有.htaccess文件的php脚本.问题是,无论我将项目放在网络服务器上的什么位置,我都希望它能工作(.htaccess文件和php-script始终位于同一文件夹中).

So I'm trying to get a mod_rewrite rule to redirect requests to a php-script with an .htaccess file. The thing is, I want it to work regardless of where I put the project on a webserver (the .htaccess file and the php-script are always in the same folder).

重写本身非常简单.如果脚本和.htacess位于目录/path/to/project 中,并且用户访问:

The rewrite itself is very simple. If the script and the .htacess are in the directory /path/to/project and the user visits:

/path/to/project/somestring

应将其重写为:

/path/to/project/index.php?t=somestring

这应该适用于Web服务器中任何级别的每个子目录.所以:

This should work for every subdirectory at any level in the webserver. So:

如果php-script和.htaccess文件位于根目录中:

If the php-script and the .htaccess files are in the root:

/somestring2

应重写为:

/index.php?t=somestring2

如果php脚本和.htaccess文件位于/子目录中:

/subdirectory/somestring3

应重写为:

/subdirectory/index.php?t=somestring3

因此,无论项目在服务器中的何处,RewriteRule都应执行相同的重写操作.要成为GET参数的字符串可以包含以下字符: [a-zA-Z0-9] .如果请求的URL中还有其他GET参数,则也应附加它们(因此有QSA标志).这是我尝试过的:

So the RewriteRule should perform the same rewrite action regardless of where the project lives within the server. The string that is to become a GET-parameter can consist of those characters: [a-zA-Z0-9]. If there are other GET-parameters in the requested URL, they should be appended as well (hence the QSA flag). This is what I've tried:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*/)([a-zA-Z0-9])/? $1index.php&t=$2 [L,QSA]

但是,这会导致404错误.我怎样才能改变它来做我想做的事?

However, this results in a 404 error. How can I alter it to do what I want?

推荐答案

尝试:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*/)?([A-Za-z0-9]+)/?$ /$1index.php?t=$2 [NC,L,QSA]

请注意,在 RewriteRule 上下文中,不需要在重写模式中使用斜杠.

Note that a leading slash in rewrite pattern is not required in the RewriteRule context.

更多推荐

未知目录的RewriteRule

本文发布于:2023-11-01 18:42:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目录   RewriteRule

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!