使用Apache mod_rewrite将所有请求发送到文件 [英] Using Apache mod_rewrite to send all requests to a file

查看:52
本文介绍了使用Apache mod_rewrite将所有请求发送到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用mod_rewrite将某些类型的传入URL发送到服务器上的特定文件?

How can I use mod_rewrite to send certain types of incoming urls to a particular file on the server?

理想情况下,任何html请求都将加载一个文件(例如/var/www/sites/urlprocessor.php),该文件将确定所请求的内容并根据url显示适当的内容.

Ideally any html requests would load a file (eg /var/www/sites/urlprocessor.php) which would then determine what is being requested and the display the appropriate content based on the url.

示例:假设我想要一个列出一些汽车"的网址.我希望以下URL加载urlprocessor.php文件,而不是加载实际页面,而不是进行重定向.

Example: Say I wanted a url to list some 'cars'. I would want the following URLs to load the urlprocessor.php file instead of loading the actual page and instead of doing a redirect.

http://www.mydomain.com/cars.html --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars.htm  --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars      --> /var/www/sites/urlprocessor.php

在反面,非html请求(jpg,pdf等)将继续正常运行,而无需加载urlprocessor.php文件.

On the flip-side, non-html requests (jpgs, pdfs, etc) would continue on normally without loading the urlprocessor.php file.

我目前正在使用以下代码的最后一行来完成此操作.它301将所有html页面请求重定向到"index.html",然后加载"urlprocessor.php"文件.我更喜欢不使用301重定向加载文件.

I am currently using the last line of code in the following to accomplish this. It 301 redirects all html page requests to 'index.html' which then loads the urlprocessor.php file. I prefer to just load the file without the 301 redirect.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule !^.*(\.gif|\.GIF|\.png|\.mp3|\.m3u|\.js|\.jpg|\.JPG|\.css|\.swf|\.flv|\.ico|\.wav|\.doc|\.DOC|\.pdf|\.PDF|\.xls|\.XLS|\.txt|\.TXT)$ index.html [L]

有没有一种方法可以直接转到文件而不进行重定向?

Is there a way to go directly to a file without doing the redirect?

谢谢!

汤姆

推荐答案

鉴于/var/www/是根文件夹,那么它将起作用:

Given that /var/www/ is the root folder then this would work:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /sites/urlprocessor.php [L]

基本上,这会将所有不存在的文件夹和文件重定向到urlprocessor.php

Basically this will redirect any non-existent folder and file to the urlprocessor.php

有关的更多信息如何使用PHP处理重定向,您可以参考此答案.

For more information on how to handle the redirect with your PHP you can refer yourself to this answer.

这篇关于使用Apache mod_rewrite将所有请求发送到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆