.htaccess 问题,需要向某些 URL 添加字符串

发布于 2024-11-07 08:28:36 字数 1167 浏览 0 评论 0原文

在我无限的愚蠢中,我不久前更改了我的 WordPress 博客上的永久链接,现在又更改了它们。我现在造成的问题是我有几百个不再有效的 URL。

例如,此 URL 看起来像以前的 URL

http://www.lazygamer.net/the-evopoints-co-za-downloads-of-the-week-1305/

但是你会发现现在只会给你一个 404 未找到页面,因为我的网站期望第一个子目录是一个类别,例如

http://www.lazygamer.net/xbox-360/the-evopoints-co-za-downloads-of-the-week-1305/

所以现在我想添加一个 htaccess 规则来检查类别是否存在,如果不存在则添加随机内容以使 url 解析。

我很确定我可以使用某种正则表达式来做到这一点,但我无法弄清楚。

[更新]我当前的.htaccess文件

RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
RewriteRule ^index\.php$ - [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^lazygamer\.net$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

In my infinity stupidity I changed the permalinks on my wordpress blog a little while back and have now changed them again. The problem that I have now caused is that I have a few hundred URL's out there which no longer work.

For example this URL looks like the ones that used to work

http://www.lazygamer.net/the-evopoints-co-za-downloads-of-the-week-1305/

But you'll see that just gives you a 404 not found page now because my site expects the first subdirectory to be a category such as

http://www.lazygamer.net/xbox-360/the-evopoints-co-za-downloads-of-the-week-1305/

So now I want to put a htaccess rule in that checks to see if a category exists and if it doesn't then just add in something random to make the url resolve.

I'm pretty sure I can do this with a regular expression of sorts but I can't figure it out.

[Update] My current .htaccess file

RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
RewriteRule ^index\.php$ - [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^lazygamer\.net$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-11-14 08:28:36

阅读 mod_rewrite 以了解如何使用它们来解决其他问题!

RewriteRule ^/([^/]+)/$   /category/$1/ [R]

应该可以做到这一点。

请注意,这会将 URL 中具有一个目录路径的所有内容重定向到 /category/{original_url_path}

Read up mod_rewrite to understand how to use these for other problems!

RewriteRule ^/([^/]+)/$   /category/$1/ [R]

That should do it.

Beware, this will redirect anything with one directory path in the URL to /category/{original_url_path}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文