发布于 2025-01-30 11:15:06 字数 1062 浏览 2 评论 0原文

我试图使我的服务器重定向到单个页面,每个请求用户询问。 我这样做的方式是:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to py-server.php if the request is that page (to avoid loops)
RewriteCond %{REQUEST_URI} !=/py-server.php
# Check if it is not an asset that the page requested needs
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
# Finally, redirects to the page by adding to the URL the path originally requested
RewriteRule ^(.*)$ /py-server.php?path=$1 [L,QSA]

我添加了一些评论,以澄清为什么我写了这些行。

现在,该代码是通过从Internet粘贴各种代码块来制作的,但是它应该从我阅读的内容中起作用。

事实是,服务器正确地将其重定向到正确的页面,但是没有url中的路径(=根本没有路径参数)。

Note .htaccess文件位于Localhost网站的根目录(我为服务器使用XAMPP);我所指的请求是,简单地是服务器的任何文件的请求,例如/index.php/some_dir//some_dir/index.php;

我忘了添加的东西吗?还是我在任何地方犯了一个错误?

I was trying to make my server redirecting to a single page every request the user asks.
The way I am doing it is this:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to py-server.php if the request is that page (to avoid loops)
RewriteCond %{REQUEST_URI} !=/py-server.php
# Check if it is not an asset that the page requested needs
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
# Finally, redirects to the page by adding to the URL the path originally requested
RewriteRule ^(.*)$ /py-server.php?path=$1 [L,QSA]

I added some comments to clarify why I've written those lines.

Now, that code is made by pasting various codeblocks from the internet, but it should work from what I read around.

The fact is that the server redirects correctly to the right page, but without the path in the URL (= there isn't the path argument at all).

Note: the .htaccess file is located at the root directory of the site in localhost (I use XAMPP for the server); the request I'm referring to is, simply, the request of any file of the server like /index.php, /some_dir/, /some_dir/index.php;

Did I forgot something to add? or did I make a mistake anywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2025-02-06 11:15:06

好吧,我一次又一次地找到了解决方案:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to py-server.php if the request is that page (to avoid loops)
RewriteCond %{REQUEST_URI} !=/py-server.php
# Check if it is not an asset that the page requested needs
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
# Here, this is the line that does the job!
RewriteRule ^(.*)$ /py-server.php?path=%{REQUEST_URI} [R=302,NC]

我通过将%{request_uri}作为原始路径的参数和r = 302标记来解决。方括号。

现在,所有这些代码都可以很好地工作:将所有请求重定向到file.php,除了资产(JS文件,图像,GIF,GIFS,CSS文件...)和实际最终文件以避免循环。

编辑:正如Mrwhite在评论中所写的那样,唯一的问题似乎是r = 302 flag,因为$ 1 and code> and % {request_uri}在两种情况下都可以使用一点。

Alright, I found the solution searching here and there again:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to py-server.php if the request is that page (to avoid loops)
RewriteCond %{REQUEST_URI} !=/py-server.php
# Check if it is not an asset that the page requested needs
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
# Here, this is the line that does the job!
RewriteRule ^(.*)$ /py-server.php?path=%{REQUEST_URI} [R=302,NC]

I solved by putting %{REQUEST_URI} as an argument for the original path to pass and the R=302 flag between squared brackets.

All this code works perfectly, now: redirects ALL the requests to a file.php EXCEPT FOR the assets (JS files, images, GIFs, CSS files...) and the actual final file to avoid loops.

Edit: as MrWhite wrote in the comments, the only problem seems to have been the R=302 flag, because $1 and %{REQUEST_URI} would work in both cases with just a little difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文