简体   繁体   English

如何重定向URL,以便它们全部由同一PHP页面处理

[英]How to redirect URLs so that they are all handled by the same PHP page

I know that there are many questions about this subject, but the questions, and even more the answers are kind of confusing me. 我知道关于这个主题有很多问题,但是这些问题,甚至更多的答案都使我感到困惑。

What I want to do: I want to have an internet page, wich, depending on the URL, shows different content. 我想做的事情:我想让一个互联网页面根据URL显示不同的内容。 However, in the backend, all pages are handled by one central PHP page. 但是,在后端,所有页面都由一个中央PHP页面处理。

In other words: 换一种说法:

www.example.com/ www.example.com/

www.example.com/AboutUs www.example.com/AboutUs

www.example.com/Contact www.example.com/Contact

should all be handled by a single .php script, but in such a way that in the browser of the users the URLS are kept intact. 应该全部由一个.php脚本处理,但应确保用户浏览器中的URL保持完整。

Now, is this done with .htaccess rewriting or not? 现在,是否通过.htaccess重写完成此操作? And how? 如何?

.htaccess using Rewrite would be the best approach for this: .htaccess使用重写将是最好的方法:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

In your index.php you can use the value of $_GET['uri'] or $_SERVER['REQUEST_URI'] to determine which functionality is being requested. index.php ,可以使用$_GET['uri']$_SERVER['REQUEST_URI']来确定正在请求的功能。

If you only want your controller script to handle requests for files and directories that don't already exist, you can do: 如果只希望控制器脚本处理对尚不存在的文件和目录的请求,则可以执行以下操作:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

Yes, you can achieve this by adding mod_rewrite rules to your .htaccess file. 是的,您可以通过向.htaccess文件中添加mod_rewrite规则来实现。 Here is an article with more detailed information: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/ . 这是一篇包含更多详细信息的文章: http : //net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

It may not help your confusion, but it will at least teach you the proper syntax. 它可能不会帮助您解决混乱问题,但至少会教您正确的语法。 Basically, mod_rewrite takes the "clean" URL given in the browser, decodes it using a regular expression, then discretely passes the matches from the regular expression as GET variables. 基本上,mod_rewrite使用浏览器中提供的“干净” URL,使用正则表达式对其进行解码,然后将来自正则表达式的匹配项作为GET变量离散地传递。

In other words: mod_rewrite takes "example.com/AboutUs", reads the URL, and serves up whatever would be on the page "example.com/index.php?page=AboutUs" without showing users the actual GET-variable-ridden URL. 换句话说:mod_rewrite使用“ example.com/AboutUs”,读取URL,并提供页面“ example.com/index.php?page=AboutUs”上的内容,而不会向用户显示实际的GET-variable URL。

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

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