简体   繁体   English

在mod_rewrite之后重定向URL?

[英]Redirecting URLs after mod_rewrite?

I'm trying to understand how the mod_rewrite works. 我试图了解mod_rewrite的工作方式。 For example, if I have a URL like: 例如,如果我有一个网址,例如:

example.com/user.php?id=123

And with the mod_rewrite I can create the URL Like: 并使用mod_rewrite可以创建如下网址:

example.com/user/123

Now this is ok, but on other locations in my application, I have linked to the user page using the old format , eg: 现在可以,但是在我的应用程序的其他位置,我已经使用旧格式链接到用户页面,例如:

<a href="user.php?id=123">123</a>

Now does it mean that I would need to change all these links manually and should link according to the new format? 现在是否意味着我需要手动更改所有这些链接,并且应该根据新格式进行链接? eg: 例如:

<a href="user/123">123</a>

Or should this be done with the mod_rewrite too? 还是应该使用mod_rewrite完成? I'm not using any PHP framework. 我没有使用任何PHP框架。

On the long run yes you would have to change it but you can also redirect that: 从长远来看,是的,您将不得不对其进行更改,但您也可以将其重定向:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Externally redirect /user.php?id=123 to /user/123
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(user)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

# Internally forward /user/123 to /user.php?id=123
RewriteRule ^user/([0-9]+)/?$ /user.php?id=$1 [NC,L]

The external redirect is the one that changes the URL on the browser, the internally is the one that does not change the URL but shows the content of the other place in question. 外部重定向是一种在浏览器上更改URL的重定向,内部重定向是一种不更改URL但显示其他地方的内容的重定向。

While the above would redirect the user from: 尽管以上内容会将用户重定向到:

example.com/user.php?id=123

To

example.com/user/123

It would be for the best if you change your links to use the SEO friendly ones to avoid the extra redirect all the time a user access or navigate on your website. 如果您将链接更改为使用SEO友好链接,那将是最好的选择,以避免每次用户访问或浏览您的网站时都进行额外的重定向。

find all links in db and replace 在数据库中找到所有链接并替换

UPDATE `myTable`
SET myCol = REPLACE(myCol, '<a href="user.php?id=', '<a href="user/')
WHERE keyCol = testKey;

to do the entire table, remove WHERE clause 做整个表,删除WHERE子句

i think that will work, i dont play with sql much nowadays 我认为这行得通,如今我不怎么使用sql了

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

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