。
我试图使我的服务器重定向到单个页面,每个请求用户询问。 我这样做的方式是:
# 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我一次又一次地找到了解决方案:
我通过将
%{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:
I solved by putting
%{REQUEST_URI}
as an argument for the original path to pass and theR=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.