如何通过重写htaccess删除URL的一部分 [英] How to remove parts of URL by rewrite in htaccess

查看:74
本文介绍了如何通过重写htaccess删除URL的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试应用在StackOverflow上找到的其他答案,但没有任何效果.

I tried to apply other answers found on StackOverflow, but nothing worked.

我在Google中有旧链接,人们正在访问,但是我的页面结构已更改,如果人们调用这些链接,该页面将显示混乱.

I have old links in Google, which people are visiting, but the structure of my page has changed and if people call those links, the page will be displayed messed up.

我有这样的链接:

/PIXMA/Canon-PRINT-Inkjet/SELPHY-664425773
/PL/SQL-Timestamp/Dawsons-Integral-Calculator-903670995
/--/-HDPrime-Dictionary-E-K/두산동아-중학교-교과서2007개정-교육-과정-429617286

我只想将最后一个斜杠后面的最后一部分保留下来,像这样:

I want to keep only the last part behind the last slash, like these:

/SELPHY-664425773
/Dawsons-Integral-Calculator-903670995
/두산동아-중학교-교과서2007개정-교육-과정-429617286

我的htaccess现在看起来像这样:

My htaccess looks like this now:

AddDefaultCharset utf-8
RewriteEngine On
Options +FollowSymlinks

RewriteRule ^robots\.txt$ robots-subdomains.txt

#ErrorDocument 404 /404.php

RewriteRule ^cat/([a-zA-Z_-]+)/([0-9]+)$ /404.php [NC]
RewriteRule ^cat/([a-zA-Z_-]+)$ /404.php [NC]
RewriteRule ^grossing/([a-zA-Z_-]+)/([0-9]+)$ /404.php [NC]
RewriteRule ^app/([a-zA-Z_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)$ /$2-$3 [R=301,NC]

#This is the final output
RewriteRule ^(.+)-([0-9]+)$ appsite.php?title=$1&id=$2

如您所见,已经有一些重写规则,我想将其重定向到404.这不是问题.

As you see, there are some rewrite rules already there, which I want to redirect to 404. Those are not the issue.

我想将那些格式错误的重定向,但仍然存在的URL重定向到htaccess中最后一行这样格式的站点.

I want to redirect those wrongly formatted, but still existing URLs redirect to the sites formatted like the last row in the htaccess.

问题是,我不知道URL中的文本和格式(斜杠数量),因为它们是即时生成的. 唯一可以确定的是URL的最后一个斜杠的最后一部分.

The problem is, I don't know the texts and formatting (the number of slashes) in the URL, because they were generated on the fly. The only certain thing is the last part of the URL behind the last slash.

有人可以告诉我,如何在最后一个斜杠之前切掉所有东西吗?

Can somebody tell me, how to cut all the things before the last slash?

推荐答案

可以使用以下方法完成:

This can be done by using:

RewriteRule ^.*/([^/]+-[0-9]+)$ /$1 [R=301]

这将重定向URL以任何数字结尾的每个请求,例如www.example.com/xxx/yyy/foobar-123-> www.example.com/foobar-123

This will redirect every request where the url ends with anything-NUMBER, e.g. www.example.com/xxx/yyy/foobar-123 --> www.example.com/foobar-123

[R=301]将强制使用HTTP 301永久移动"进行重定向.这样可以告诉所有搜索引擎,他们应该删除旧的URL,而改用新的URL.

[R=301] will force a redirect with HTTP 301 "Moved permanently". That will tell all search engines that they should delete the old URLs and instead use the new ones.

这篇关于如何通过重写htaccess删除URL的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆