mod_rewrite的去除PHP扩展和preserve GET参数参数、PHP、mod_rewrite、GET

2023-09-02 00:41:47 作者:别殷勤

我已经看过这些解决方案,其中没有实际工作正常在我结束:

mod_rewrite删除的.php,但仍服务于PHP文件?

How从网站地址删除文件扩展名?

How使用URL重写,除去PHP扩展,而preserving的GET参数?

和他们只是没有做到以下几点,这是我需要做的:

1)从文件中删除PHP扩展和代替的index.php显示/指数

2)preserve GET参数(这是我的会话cookie读取和存储在加载该文件,在头),所以不是/index.php?a=1&b=2显示说不定/指数/ A1 / B2

3)子域和https工作://没有完全搞乱了URL或无限循环或某事结束了......

有没有人有一个线索如何放在一起,使他们支付高于正常的3个点,这些规则是什么?

这就是我与我的工作为出发点:

 的RewriteCond%{} THE_REQUEST(的.php(。*) SHTTP / 1)
重写规则^(。+)。PHP / $ 1 [R = 301,L,QSA]
 
THINKPHP扩展PHPEXCEL,PHP7.2以上版本无法导出Excel

解决方案

您现在看到的这一轮错误的方式。你不想重定向一个PHP文件到一个干净的地址的请求,因为你永远不希望用户看到的第一个地方的.php。相反,你需要采取不与.PHP结束,然后检查文件(表面下)是否真的具有.php扩展名的请求。然后,如果是这样的话,默默地改写了要求增加PHP扩展,从而使用户永远不需要看到的分机。

请尝试以下操作:

 的RewriteCond%{DOCUMENT_ROOT} / $ 1.PHP -f
重写规则^([A-ZA-Z0-9 _-] +)/ $ /$1.php [QSA]
 

本的RewriteCond检查是否请求(上涨到DOCUMENT_ROOT路径末端)形成有效的文件,一旦.PHP被添加到它的结束。如果是这样,那么重写规则允许静默重写请求,使.PHP被添加到请求的路径。该QSA标志告诉mod_rewrite的追加现有的查询字符串到新的请求路径的末尾,所以你的GET变量应该是preserved。

需要注意的是DOCUMENT_ROOT可能会或可能不会有斜线了,所以你可能需要斜杠前 $ 1 来摆脱在RewriteCond指令。

作为查询字符串参数,mod_rewrite的无法处理的变量数不确定,所以你需要一个mod_rewrite的解决方案之前,给你的问题​​一个明确的请求模式可以提供一种方法,它映射到一个查询字符串。

I have looked at these solutions and none of them actually work properly on my end:

mod_rewrite to remove .php but still serve the .php file?

How to remove file extension from website address?

How to use Apache Mod_rewrite to remove php extension, while preserving the GET parameters?

and they just don't do the following, which is what I need done:

1) remove .php extension from files and instead of /index.php display /index

2) preserve GET parameters (which I read and store in session cookies while the file is being loaded, in the header), so instead of /index.php?a=1&b=2 display maybe /index/a1/b2

3) work on subdomains and https:// without messing up the URL completely or ending up in an infinite loop or something...

Does anyone have a clue how to put together these rules so they cover the 3 points above correctly?

This is what I'm working with as a starting point:

RewriteCond %{THE_REQUEST} (.php(.*)sHTTP/1)
RewriteRule ^(.+).php$ /$1 [R=301,L,QSA]

解决方案

You're seeing this the wrong way round. You don't want to redirect a request for a .php file to a clean address, because you never want the user to see the .php in the first place. Instead you need to take a request which does not end with .php and then check to see whether the file (underneath the surface) actually has a .php extension. Then, if that's the case, silently rewrite the request to add the .php extension, so that the user never needs to see the extension.

Try the following:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /$1.php [QSA]

The RewriteCond checks to see whether the request (tacked on to the end of the DOCUMENT_ROOT path) forms a valid filename once ".php" is added to the end of it. If so, then the RewriteRule is allowed to silently rewrite the request so that ".php" is added to the request path. The QSA flag tells mod_rewrite to append the existing query string to the end of the new request path, so your GET variables should be preserved.

Note that DOCUMENT_ROOT may or may not have a trailing slash already, so you might need to get rid of the slash before $1 in the RewriteCond.

As for query string parameters, mod_rewrite cannot handle an indefinite number of variables, so you would need to give an explicit request pattern in your question before a mod_rewrite solution could offer a way to map it to a query string.