.htaccess 问题,需要向某些 URL 添加字符串
在我无限的愚蠢中,我不久前更改了我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读 mod_rewrite 以了解如何使用它们来解决其他问题!
应该可以做到这一点。
请注意,这会将 URL 中具有一个目录路径的所有内容重定向到
/category/{original_url_path}
Read up mod_rewrite to understand how to use these for other problems!
That should do it.
Beware, this will redirect anything with one directory path in the URL to
/category/{original_url_path}