htaccess 重写仍然存在一些问题,变得有点复杂了 eek!

发布于 2024-11-02 00:29:57 字数 3533 浏览 0 评论 0原文

我的 .htaccess 重写传奇的另一个版本。现在一切都在一定程度上发挥作用,但仍然存在一些问题。

我有很多规则可以将旧网址移动到新网址、传递变量等 - 但是仍然有一些东西我需要添加,而且我一生都无法弄清楚。

我有 3 个正在定向/重写的网址,如下所示。


1 www.mydomain.com/news/dentistry_dental/index.php

2 www.mydomain.com/news/dentistry_dental/index.php?month=April&year=2011

3 www.mydomain.com/news/dentistry_dental/article_detail.php ?article=1234&title=some-title


这些被重定向 &分别完美重写为新网址


1 www.mydomain.com/dental_news/

2 www.mydomain.com/dental_news/April-2011

3 www.mydomain.com/dental_news/1234-some-title


但是... 这里问题是#1 以下网址也重定向如下


4 www.mydomain.com/news/it_technology/index.php?month=April&year=2011

5 www.mydomain.com/news/it_technology/article_detail.php?article=1234&title=some-title


也指向与牙科重定向相同的网址


4 www.mydomain.com/dental_news/April-2011

5 www.mydomain.com/dental_news/1234-some-title


这不应该发生。 it_technology 新闻文章现已被删除,因此我希望使用 410 或类似的内容将它们重定向到我的主页,以最佳选择为准。

我当前的 .htaccess 如下所示。

选项 +FollowSymlinks RewriteEngine

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

    # Rewrite all index.php to root: / ( with perm redirect )
    RewriteCond %{REQUEST_URI} !/generator/
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

    RewriteRule ^products/dental-digital-imaging/([^/]*)_([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
    RewriteRule ^products/package_deals.php$ http://www.mydomain.com/products/dental-computer-network-bundles [R=301]
    RewriteRule ^products/computer_hardware.php$ http://www.mydomain.com/products/dental-computer-solutions [R=301]
    RewriteRule ^products/individual_computers.php$ http://www.mydomain.com/products/dental-computer-systems [R=301]
    RewriteRule ^products/digital_xray_imaging.php$ http://www.mydomain.com/products/dental-digital-imaging [R=301]

    RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]

    # Rewrite dental news article to neat nice url
    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([0-9]*)-([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]

    #Conditional rewrite of old news article path to new one with 301 redirect
    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([a-zA-Z]*)-([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]

    # Rewrite URL stripping .php Extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

我唯一的其他要求是在提供的 php 文件中添加一个尾部斜杠(这些文件已经添加了 .php 扩展名,并在我的 .htaccess 中删除了最后一条规则。

我已经尝试了通过谷歌找到的多种方法,但是他们都对我的其他规则提出了问题,

希望有人能帮助我一劳永逸地完成这个任务

。 中号

Another edition to my .htaccess rewriting saga. Everything is now working to a degree but still have some issues.

I have numerous rules to move old urls to new urls, passing variables etc - however there are still a couple of things i need to add, and for the life of me cannot figure out.

I have 3 urls that are directing/rewriting as below.


1 www.mydomain.com/news/dentistry_dental/index.php

2 www.mydomain.com/news/dentistry_dental/index.php?month=April&year=2011

3 www.mydomain.com/news/dentistry_dental/article_detail.php?article=1234&title=some-title


These are redirected & rewritten perfectly to the new urls respectively


1 www.mydomain.com/dental_news/

2 www.mydomain.com/dental_news/April-2011

3 www.mydomain.com/dental_news/1234-some-title


However... Here is the problem #1 The following urls are also redirecting as below


4 www.mydomain.com/news/it_technology/index.php?month=April&year=2011

5 www.mydomain.com/news/it_technology/article_detail.php?article=1234&title=some-title


Which are also directing to the same urls as the dental redirects


4 www.mydomain.com/dental_news/April-2011

5 www.mydomain.com/dental_news/1234-some-title


Which shouldnt be happening. The it_technology news articles have now been removed so i wish to redirect them either to my homepage with a 410 or something similar, whichever is the best option really.

My current .htaccess looks as follows.

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

    # Rewrite all index.php to root: / ( with perm redirect )
    RewriteCond %{REQUEST_URI} !/generator/
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

    RewriteRule ^products/dental-digital-imaging/([^/]*)_([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
    RewriteRule ^products/package_deals.php$ http://www.mydomain.com/products/dental-computer-network-bundles [R=301]
    RewriteRule ^products/computer_hardware.php$ http://www.mydomain.com/products/dental-computer-solutions [R=301]
    RewriteRule ^products/individual_computers.php$ http://www.mydomain.com/products/dental-computer-systems [R=301]
    RewriteRule ^products/digital_xray_imaging.php$ http://www.mydomain.com/products/dental-digital-imaging [R=301]

    RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]

    # Rewrite dental news article to neat nice url
    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([0-9]*)-([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]

    #Conditional rewrite of old news article path to new one with 301 redirect
    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([a-zA-Z]*)-([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]

    # Rewrite URL stripping .php Extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

My only other requirement would be to add a trailing slash to the php files served ( that have allready add the .php extension removed with the last rule in my .htaccess.

I have tried numerous ways that i have found via google, but all of them cuase probelms with my other rules.

Hopefully someone can help me finish this off once and for all.

Regards
M

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

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

发布评论

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

评论(2

暗喜 2024-11-09 00:29:57

当然变得很复杂:) 尝试更改这些行(我认为有 2 行):

RewriteCond %{REQUEST_URI} !^/dental_news/

到:

RewriteCond %{REQUEST_URI} ^/news/dentistry_dental/

这样,它只会将旧的牙科新闻 URL 重定向到相应的新 URL。

Sure is getting complicated :) Try changing these lines (I think there are 2):

RewriteCond %{REQUEST_URI} !^/dental_news/

to:

RewriteCond %{REQUEST_URI} ^/news/dentistry_dental/

That way its only going to redirect the old dental news URLs to there respective new ones.

白馒头 2024-11-09 00:29:57

简单地说,您的问题是包罗万象的规则:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]

将它们限制为您想要重写的路径,即将 .* 替换为更具体的内容,例如:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [R=301]

您可以执行 news/it_technology< /code> 重定向作为标准重定向。

RewriteRule ^news/it_technology http://www.thepage.com/ [R=301,L]

从技术上讲,执行 410 可能更正确,但 410 代码实际上是可选的。您可能还想将其设为 404,然后有一个从 404 页面到主页的链接。

Trivially, your problem is the catch-all rules:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]

Restrict them to the path you want to rewrite, i.e. replace .* with something more specific, like:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [R=301]

You can do the news/it_technology redirect as a standard redirect.

RewriteRule ^news/it_technology http://www.thepage.com/ [R=301,L]

It might technically be more correct to do a 410, but the 410 code is really optional. You might also want to make it 404, and then have a link from the 404 page to the main page.

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