简体   繁体   English

htaccess 重定向与 php 重定向

[英]htaccess redirect vs php redirect

I have decided to change all my /dyanmic.php?UID=... pages to /static/Name-From-DB .我决定将我所有的/dyanmic.php?UID=...页面更改为/static/Name-From-DB I have read that the best way to do redirection, SEO wise, is to implement a 301 .htacess redirection:我已经读过,在 SEO 方面进行重定向的最佳方法是实现 301 .htacess 重定向:

(http://www.tamingthebeast.net)- The right way - a htaccess 301 Redirect: A 301 redirect is the most efficient and spider/visitor friendly strategy around for web sites that are hosted on servers running Apache. (http://www.tamingthebeast.net)- 正确的方法 - htaccess 301 重定向:对于托管在运行 Apache 的服务器上的网站,301 重定向是最有效和蜘蛛/访问者友好的策略。

However, since I have thousands of pages I need to use redirection for, it seems pretty inefficient to have thousands of entries in the .htacess file:但是,由于我有数千个页面需要使用重定向,因此在 .htacess 文件中包含数千个条目似乎非常低效:

redirect 301 /dynamid.php?UID=1 http://www.domain.com/static/Name-From-DB
redirect 301 /dynamid.php?UID=2 http://www.domain.com/static/Another-Name-From-DB
and so on...

Therefore it seems like the efficient way to do it is via the PHP page, setting the header to 301 redirection according to the UID:因此,似乎有效的方法是通过 PHP 页面,根据 UID 将标头设置为 301 重定向:

<?
Header( "HTTP/1.1 301 Moved Permanently" ); 
// Getting the page static name from the DB according to the UID
$result = mysql_query('SELECT Name FROM DB WHERE UID='$_GET["uid"]'');
$row=mysql_fetch_assoc($result);
// Redirect to the new page
Header( "Location: http://www.domain.com/static/" . $row[0] ); 
?> 

My question is if this method will effect in a negative way on my pages' rankings, considering the paragraph I quoted above that the best way to do a redirection is through the .htaccess.我的问题是这种方法是否会对我的页面排名产生负面影响,考虑到我上面引用的段落,进行重定向的最佳方法是通过 .htaccess。

Thanks!谢谢!

The same headers will be sent the same whichever way you do it.无论您采用哪种方式,都会以相同的方式发送相同的标头。 Therefore, to the user (or to a search engine) it won't be any different so the PHP won't negatively effect your page rank compared to the .htaccess way.因此,对于用户(或搜索引擎)来说,它不会有任何不同,因此与 .htaccess 方式相比,PHP 不会对您的页面排名产生负面影响。

The PHP way will be a little bit slower as every time the page is accessed a database query is fired off. PHP 方式会慢一点,因为每次访问页面时都会触发数据库查询。 If the site doesn't have too much traffic going to those pages it shouldn't be an issue though.如果该站点没有太多流量进入这些页面,那么它应该不是问题。

In your PHP page, do the database connection first because if the page doesn't exist you will want to either redirect them to another page or pass 404 error headers instead of the 301 header.在您的 PHP 页面中,首先进行数据库连接,因为如果该页面不存在,您将希望将它们重定向到另一个页面或传递 404 错误标头而不是 301 标头。

My question is if this method will effect in a negative way on my pages' rankings, considering the paragraph I quoted above that the best way to do a redirection is through the .htaccess.我的问题是这种方法是否会对我的页面排名产生负面影响,考虑到我上面引用的段落,进行重定向的最佳方法是通过 .htaccess。

No , it will not provided that you implement the redirection correctly in your PHP file!,它不会提供您在 PHP 文件中正确实现重定向的条件! Ie, you should probably look up the Name (by uid ) before issuing the 301 header (and only issue this when a Name is found — otherwise it's a 404 ).即,您可能应该发出301标头之前查找Name (通过uid )(并且在找到Name时才发出此信息 - 否则它是404 )。

Also, why don't you run a query on your database, output the redirects in .htaccess format and write this directly to the .htaccess file?另外,为什么不在数据库上运行查询,以.htaccess格式输出重定向并将其直接写入.htaccess文件? This way you'll have to do it only once and wont have to care about non-existing uid 's.这样你只需要做一次,而不必关心不存在的uid


Edit — pseudocode编辑——伪代码

str ← ""
for each (uid, Name) in database:
    line ← concat("Redirect 301 /dynamid.php?UID=", uid, "http://www.domain.com/static/", Name, <newline>)
    append line to str
copy or write str to .htaccess

Doing this, you'll still be able to find your redirects in a year (they're all in .htaccess) and there's no longer a need to do a look-up in the database on every "old style" request.这样做,您仍然可以在一年内找到您的重定向(它们都在 .htaccess 中),并且不再需要对每个“旧式”请求在数据库中进行查找。

No. The client sees the same thing in both cases: a 301 redirect.不。客户端在两种情况下看到的都是相同的:301 重定向。 The only difference could be delay, since the .htaccess is processed before the php file, and the sql query may add some delay, but I think the delay insignificantly small.唯一的区别可能是延迟,因为.htaccess是在php文件之前处理的,sql查询可能会增加一些延迟,但我认为延迟很小。

It won't affect your pageranking - Google appreciates when you tell them your pages has moved and that you won't maintain your site wíth duplicate content.它不会影响您的网页排名 - 当您告诉 Google 您的网页已移动并且您不会用重复的内容维护您的网站时,Google 会很感激。

But as said in other answers, the header is the same wether you send it from PHP or through Apache - your focus should be how to minimize your work;但正如其他答案中所说,无论您是从 PHP 还是通过 Apache 发送,标头都是相同的——您的重点应该是如何最大限度地减少您的工作; both now and in the future.无论是现在还是将来。

By manually typing the redirect in .htaccess you actually give yourself more work then necessary when comparing to dynamically adding headers through your PHP when needed.通过在 .htaccess 中手动输入重定向,与在需要时通过 PHP 动态添加标头相比,您实际上给了自己更多必要的工作。

...the best way to do a redirection is through the .htaccess... ...进行重定向的最佳方法是通过 .htaccess ...

If you are on a shared server, that may be the case, but, in reference to a long or even a short list of files, that .htaccess file is going to be searched for with every file access within your page - Every graphic, every include file, etc.如果您在共享服务器上,情况可能是这样,但是,对于很长甚至很短的文件列表,将在页面中的每个文件访问中搜索该 .htaccess 文件 - 每个图形,每个包含文件等

Not only that, but, if you are a few levels deep, it checks for .htaccess files in all parent directories.不仅如此,如果您深入几层,它还会检查所有父目录中的 .htaccess 文件。 So, if your file is in home/dir1/dir2/dir3, it's going to check for .htaccess files in each directory with each file access:因此,如果您的文件在 home/dir1/dir2/dir3 中,它将使用每个文件访问权限检查每个目录中的 .htaccess 文件:
home/dir1/dir2/dir3/家/dir1/dir2/dir3/
home/dir1/dir2/家/目录1/目录2/
home/dir1/家/目录1/
home/家/

Best to keep it as light as possible.最好保持它尽可能轻。 If you are not on a shared server, you should not use .htaccess files at all.如果您不在共享服务器上,则根本不应使用 .htaccess 文件。 Enter any directives into the main configuration file, under Directory, and set AllowOverride to "none".在主配置文件的目录下输入任何指令,并将 AllowOverride 设置为“none”。

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

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