简体   繁体   English

PHP如何将URL路径重定向到index.php

[英]PHP how to redirect url path to index.php

I currently have a website with URLs with this pattern 我目前有一个网址带有这种格式的网站

https://website.com/?p=something

where p is the requested page (like /?p=login) 其中p是请求的页面(例如/?p = login)

for SEO reasons i want the URL to look like this 由于SEO的原因,我希望URL看起来像这样

https://website.com/login/

Of course I could do a redirect like this 我当然可以这样重定向

Redirect 301 /login/ https://website.com/?p=login

But it would change the URL displayed in the users browser back to the URL with parameters in it. 但这会将用户浏览器中显示的URL更改回带有参数的URL。

My question is, how can I have an URL like this https://website.com/login/ but in PHP inside the index.php I could access the requestet page or "path" like before like $page = $_GET['p']; 我的问题是,我如何拥有这样的URL,例如https://website.com/login/但是在PHP中,在index.php内,我可以像以前一样访问请求页面或“路径”,例如$page = $_GET['p']; . In other words I want to redirect the URL https://website.com/login/ to https://website.com/?p=login but having the URL still displayed as https://website.com/login/ . 换句话说,我想将URL https://website.com/login/重定向到https://website.com/?p=login但该URL仍显示为https://website.com/login/

I'd suggest to use .htaccess file to point all requests to the server to one file for example index.php 我建议使用.htaccess文件将所有对服务器的请求指向一个文件,例如index.php

# Rewrite Engine ON
RewriteEngine on

# Redirect everything to one file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

After that you can code a router to handle URI components. 之后,您可以对路由器进行编码以处理URI组件。

For example someone call sitename/login 例如有人打电话给站点名称/登录

You detect /login and open the required page 您检测到/ login并打开所需的页面

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

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