htaccess 301 用于多个域和以空格命名的文件

发布于 2024-12-11 22:18:47 字数 495 浏览 0 评论 0原文

我有一个客户,他运行着一组成功的网站,这些网站是用原始 html 构建的。他现在觉得应该将其数据库化。

我有两个问题,我想知道是否有一个正则表达式可以容纳它们。

我希望他的网站在一个位置运行但使用多个域。

他还用空格命名了所有文件。

举例来说...

我运行该网站 www.maindomain.com

有人进入 www.domain1.com/first Category.html

我想要 301 重定向到 www.domain1.com/categories/first-category

或有人进入 www.domain2.com/second Category.html

我想要 301 重定向到 www.domain2.com/catergories/second-category

这可以实现吗?我的客户有数千个这样的页面,所以我不想要一个带有单独 301 重定向的巨大 htaccess 文件

I have a client who runs a successful set of websites which he has built with raw html. He now feels it should be databased.

There are two issues I have and I was wondering if there is a regular expression to accommodate them both.

I want his website to run in the one location but use multiple domains.

Also he has named all his files with spaces.

So for example...

I run the site on
www.maindomain.com

Somebody enters on
www.domain1.com/first category.html

and I want a 301 redirect to
www.domain1.com/categories/first-category

or somebody enters on
www.domain2.com/second category.html

and I want a 301 redirect to
www.domain2.com/catergories/second-category

Is this achievable? My client has thousands of these pages so I dont want a huge htaccess file with individual 301 redirects

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

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

发布评论

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

评论(1

披肩女神 2024-12-18 22:18:47

使用 mod_rewrite,您将需要 2 组重写。首先将空格更改为内部重定向的破折号 (-):

RewriteCond %{REQUEST_URI} "\ "
RewriteRule ^(.*)\ (.*)$ $1-$2 [L]

这将循环直到没有更多空格,因此“www.domain1.com/first Category.html”在内部被重写为“www.domain1.com/first-”类别.html”。然后我们删除末尾的“.html”并在开头插入“/categories/”:

# We need to make sure it has no spaces
RewriteCond %{REQUEST_URI} !"\ "
# and check that the request ends with ".html"
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*).html /categories/$1 [L,R=301]

然后将内部“www.domain1.com/first-category.html”请求重定向(使用301)到“www.domain1.com/first-category.html”。 domain1.com/categories/first-category”。现在的问题是,“/categories/”与以“category.html”结尾的原始url有什么关系吗?如果是这样,那么您将需要一些额外的重写。

Using mod_rewrite, you'll need 2 sets of rewrites. First to change the spaces to dashes (-) that INTERNALLY redirect:

RewriteCond %{REQUEST_URI} "\ "
RewriteRule ^(.*)\ (.*)$ $1-$2 [L]

This will loop until there are no more spaces, so "www.domain1.com/first category.html" gets rewritten internally as "www.domain1.com/first-category.html". Then we remove the ".html" at the end and insert a "/categories/" in the beginning:

# We need to make sure it has no spaces
RewriteCond %{REQUEST_URI} !"\ "
# and check that the request ends with ".html"
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*).html /categories/$1 [L,R=301]

This then redirects (with 301) the internal "www.domain1.com/first-category.html" request to "www.domain1.com/categories/first-category". Now the question is, does the "/categories/" have anything to do with the original url ending with "category.html"? If so, then you're going to need some additional rewrites.

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