简体   繁体   English

301在动态PHP网站中使用.htaccess重定向

[英]301 redirect with .htaccess in dynamic PHP website

I wanted to ask you a question about 301 URL redirection. 我想问一个关于301 URL重定向的问题。 So, I have a website and it's former URL used to be: 因此,我有一个网站,它的前URL曾经是:

http://www.sample.com/tutorials.php?name=sample

But, now it's: 但是,现在是:

http://www.sample.com/tutorials/programming/sample.php

So, you can conclude that the syntax of the new URL is this: 因此,您可以得出结论,新URL的语法是这样的:

http://www.sample.com/tutorials/(name of the category)/(value of the variable).php

I've been toying around with 301 redirect, but I got 500 Internal server error when I used this code: 我一直在玩301重定向,但是使用此代码时出现500内部服务器错误:

RewriteEngine On
RewriteCond %{COND ^tutorials.php?name=sample$ HTTP/1.1} ^[A-Z]{3,9}\ /tutorials\.php\?name=([^&\ ]+)
RewriteRule ^ /tutorials/programming/%1.php [L,R=301]

I have also tried to create my own 301 redirect code, which gives me no 500 Internal server error, but I don't know will it work (I can't test it on localhost, because search engines don't index localhost). 我还尝试创建自己的301重定向代码,该代码不会给我500内部服务器错误,但我不知道它是否可以工作(我无法在localhost上对其进行测试,因为搜索引擎不会为localhost编制索引)。 Here it is : 这里是 :

RewriteEngine On
Redirect 301 /tutorials.php?name=$1 http://www.sample.com/tutorials/programming/(.+)\.php

So, if my code above is good than great, but if it's not, can someone please explain (as slowly and as detailed as possible, please) how to 301 redirect my old site to my new one. 因此,如果上面的代码不错,但是不好,请有人解释(请尽可能缓慢和详细地介绍)如何301将我的旧站点重定向到我的新站点。 As I mentioned, I use dynamic web-sites and I am rewritting using .htaccess file (mod_rewrite). 如前所述,我使用动态网站,并且使用.htaccess文件(mod_rewrite)进行改写。

I have tried to be as detailed as possible. 我试图尽可能详细。 If you need additional details, feel free to ask. 如果您需要其他详细信息,请随时询问。

You have to redirect the old links to your new domain. 您必须将旧链接重定向到新域。 This is how you can do it. 这就是您可以做到的。

Using htaccess : 使用htaccess

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain. 使用下面的代码创建一个.htaccess文件,它将确保您旧域的所有目录和页面都将正确重定向到新域。 The .htaccess file needs to be placed in the root directory of your old website (ie the same directory where your index file is placed) .htaccess文件需要放置在旧网站的根目录中(即,放置索引文件的目录)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] 

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website. 除了重定向外,我建议您联系每个反向链接站点以修改其反向链接以指向您的新网站。

Note: This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled. 注意:这种.htaccess重定向方法仅在启用Apache Mod-Rewrite模块的Linux服务器上有效。

Refer this for more methods 参阅以获得更多方法

EDIT: 编辑:

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com The .htaccess file needs to be placed in the root directory of your old website (ie the same directory where your index file is placed) 使用以下代码创建一个.htaccess文件,它将确保所有进入domain.com的请求都将重定向到www.domain.com。.htaccess文件需要放置在您旧网站的根目录中(即放置索引文件的目录)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 

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

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