简体   繁体   English

如何使用.htaccess 在 PHP 中重定向 url

[英]How can I redirect url in PHP using htaccess

I need some rewrite rules for URLs我需要一些 URL 重写规则

I want:我想:

www.dawsonautodetailing.com/?page=About-Us

Changed to:变成:

www.dawsonautodetailing.com/page/About-Us or www.dawsonautodetailing.com/About-Us www.dawsonautodetailing.com/page/About-Uswww.dawsonautodetailing.com/About-Us

I've already created .htaccess into my root folder.我已经在我的根文件夹中创建了 .htaccess。

Thank you for the help!感谢您的帮助!

I've tried to google it but since i am new to .htaccess i don't even know where to begin:)我试过用谷歌搜索,但由于我是 .htaccess 的新手,我什至不知道从哪里开始:)

You can use the mod_rewrite module in Apache to redirect URLs in PHP using .htaccess.您可以使用 Apache 中的 mod_rewrite 模块使用 .htaccess 重定向 PHP 中的 URL。

To redirect a specific URL, you can use the following syntax in your .htaccess file:要重定向特定的 URL,您可以在 .htaccess 文件中使用以下语法:

RewriteEngine On  

RewriteRule ^old-url$ http://www.example.com/new-url [R=301,L]

This will redirect any requests for the URL "old-url" to "http://www.example.com/new-url" with a "301" redirect (permanent redirect).这会将对 URL“old-url”的任何请求重定向到“http://www.example.com/new-url”,并使用“301”重定向(永久重定向)。

You can also use regular expressions to match patterns in the URL and redirect accordingly.您还可以使用正则表达式来匹配 URL 中的模式并相应地重定向。 For example, if you want to redirect all requests for URLs starting with "old-directory" to "new-directory", you can use the following:例如,如果要将所有以“old-directory”开头的 URL 请求重定向到“new-directory”,可以使用以下方法:

RewriteEngine On

RewriteRule ^old-directory/(.*)$ http://www.example.com/new-directory/$1 [R=301,L]

This will take any URL starting with "old-directory" and redirect it to the same URL, but starting with "new-directory" instead.这会将任何以“old-directory”开头的 URL 重定向到相同的 URL,但以“new-directory”开头。

Be careful while modifying.htaccess as it can cause website to malfunction.修改 .htaccess 时要小心,因为它可能会导致网站出现故障。 It is always a good practice to take backup before making any changes.在进行任何更改之前进行备份始终是一个好习惯。

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

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