简体   繁体   English

将所有内容重定向到 index.php 而不更改 url

[英]Redirect everything to index.php without changing url

I am getting crazy trying to get this work.试图获得这项工作我快疯了。 I am planning a rather complex redesign of my whole website.我正在计划对我的整个网站进行相当复杂的重新设计。 Since my website should work differently depending on which subfolder (eg www.mydomain.com/test123) you try to access, I think the easiest solution would be to redirect every single request to a main index.php and handle all cases from there.由于我的网站应该根据您尝试访问的子文件夹(例如 www.mydomain.com/test123)以不同的方式工作,我认为最简单的解决方案是将每个请求重定向到主 index.php 并从那里处理所有情况。 I will also want to redirect images and css files etc. as I will handle those cases "manually" too.我还想重定向图像和 css 文件等,因为我也会“手动”处理这些情况。

Here exactly what it should do:这正是它应该做的:

  1. It should redirect every request to index.php in the root folder它应该将每个请求重定向到根文件夹中的 index.php
  2. It should pass everything after the first slash as a query param.它应该将第一个斜杠之后的所有内容作为查询参数传递。 (mydomain.com/test123 --> mydomain.com/index.php?q=test123) (mydomain.com/test123 --> mydomain.com/index.php?q=test123)
  3. This redirection should not be visible for the client.客户端不应看到此重定向。 In the above example this means it should stay /test123 but the server should receive /index.php?q=test123在上面的例子中,这意味着它应该保留 /test123 但服务器应该接收 /index.php?q=test123

I have tried this but this wont work somehow...我试过这个,但这不会以某种方式工作......

RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ /index.php?q=$1 [P]

Any help is greatly appreciated!任何帮助是极大的赞赏!

This rule should work for you in site root .htaccess:此规则应该适用于站点根目录 .htaccess:

DirectorySlash Off
DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.+)$ /index.php?q=$1 [L,QSA]

No need to use P flag that is used for proxying and that requires mod_proxy to be enabled in Apache config.无需使用用于代理的P标志,并且需要在 Apache 配置中启用mod_proxy

You can use the include() function in php.您可以在 php 中使用 include() 函数。

ex前任

<?php
include('www.google.com');
?>

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

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