简体   繁体   English

.htaccess带有随机参数的URL重写

[英].htaccess URL rewrite with random parameters

I'm currently writing a PHP app and am about to create various controllers. 我目前正在编写一个PHP应用程序,并且即将创建各种控制器。 Currently I have one controller with certain method, so the URL looks like this: 目前,我有一个具有特定方法的控制器,因此URL如下所示:

http://somewebsite.com/index.php?c=controller&a=action

It's being rewritten to this: 它被重写为:

http://somewebsite.com/controller/action

With this .htaccess file: 使用此.htaccess文件:

RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?c=$1&a=$2 [NC]

What I want to achieve is the ability to rewrite URL with more than one controller (the more, the better), possibly in random order. 我要实现的功能是可以使用多个控制器(越多越好)重写URL,并且可能以随机顺序进行。 Is there a more convenient way than rewriting every possible combination of URL parameters? 是否有比重写URL参数的所有可能组合更方便的方法?

Many frameworks (Codeigniter, WordPress, Laravel, etc.) use an .htaccess file similar to the following: 许多框架(Codeigniter,WordPress,Laravel等)都使用类似于以下内容的.htaccess文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

This rewrites all incoming URLs to be handled by the index.php file. 这将重写由index.php文件处理的所有传入URL。 You can then use $_SERVER['REQUEST_URI'] variable to get the exact request URI, parse it, and then handle it how you want. 然后,您可以使用$_SERVER['REQUEST_URI']变量来获取确切的请求URI,对其进行解析,然后根据需要进行处理。

如果您要使用任意路由方案,则最好的选择是将整个url字符串(例如^(。+)$)传递给index.php,然后让您的php应用程序基于'/'分割字符串并处理但是数组对您的应用程序有意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM