如何使用一个RewriteRule .htaccess来处理所有请求

时间:2021-07-28 10:38:44

I'm working on a web application that requires using .htaccess for URL rewriting. I want to pass all request to a single file. How can i use a single RewriteRule to achieve this?

我正在开发一个Web应用程序,需要使用.htaccess进行URL重写。我想将所有请求传递给单个文件。我如何使用单个RewriteRule来实现这一目标?

2 个解决方案

#1


This will work perfectly fine for you

这对你来说非常好

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /path/to/file [L]

The RewriteCond ensures the static files aren't rewritten (ie. robots.txt, favicon.ico, img/potato.jpg)

RewriteCond确保不重写静态文件(即robots.txt,favicon.ico,img / potato.jpg)

#2


You can use a RewriteRule that always matches:

您可以使用始终匹配的RewriteRule:

RewriteRule ^ /path/to/the/file [L]

You might want to add a RewriteCond as well. For example:

您可能还想添加RewriteCond。例如:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /app.php [L]

The RewriteCond ensures that urls that map to an actual static file in your web root do not get rewritten (ie. robots.txt, favicon.ico, img/potato.jpg )

RewriteCond确保映射到Web根目录中的实际静态文件的URL不会被重写(即robots.txt,favicon.ico,img / potato.jpg)

#1


This will work perfectly fine for you

这对你来说非常好

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /path/to/file [L]

The RewriteCond ensures the static files aren't rewritten (ie. robots.txt, favicon.ico, img/potato.jpg)

RewriteCond确保不重写静态文件(即robots.txt,favicon.ico,img / potato.jpg)

#2


You can use a RewriteRule that always matches:

您可以使用始终匹配的RewriteRule:

RewriteRule ^ /path/to/the/file [L]

You might want to add a RewriteCond as well. For example:

您可能还想添加RewriteCond。例如:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /app.php [L]

The RewriteCond ensures that urls that map to an actual static file in your web root do not get rewritten (ie. robots.txt, favicon.ico, img/potato.jpg )

RewriteCond确保映射到Web根目录中的实际静态文件的URL不会被重写(即robots.txt,favicon.ico,img / potato.jpg)