【问题标题】:htaccess URL redirectionhtaccess URL 重定向
【发布时间】:2013-08-01 17:33:48
【问题描述】:

如何将一些 URL 重定向到不同的格式。

http://mysite.com/mydir1/mydir2/my-article-first-86974.html
http://mysite.com/mydir1/somefolder/my-article-76674.html
http://mysite.com/mydir1/anotherfolder/some-text-35667.html
http://mysite.com/mydir1/mydir6/my-article-another-75445.html

我想将以上网址重定向到以下格式。

http://mysite.com/mydir2/my-article-first-1-86974.html
http://mysite.com/somefolder/my-article-1-76674.html
http://mysite.com/anotherfolder/some-text-1-35667.html
http://mysite.com/mydir6/my-article-another-1-75445.html

提前致谢。

【问题讨论】:

  • 在线查找基本的 htaccess 教程?这看起来一点也不复杂......你尝试过什么?
  • 我对我的问题进行了一些更改,但在任何教程中都找不到解决方案。

标签: php .htaccess url mod-rewrite


【解决方案1】:

应该是这样的:

RewriteEngine on 
RewriteRule ^mydir(.*)/(.*)/([a-z\-]{2,}([a-z\-][\d])[\d])(.*)$ $2/$3$1-$4 [R=301,L]

【讨论】:

  • 谢谢。我不能在规则中使用“我的文章”这个词,因为它在所有 URL 中都会有所不同。请查看相关的新变化。
  • 感谢您的回复,但它不起作用,它没有在数字前的url中添加'-1'。即 my-article-first-86974.html 应该更改为 my-article-first-1-86974.html,它没有发生。
  • 我尝试了您给出的最新规则,但它没有对 URL 进行任何更改。
  • 没有1的URL重定向?喜欢:my-article-first-86974.html?
  • @Saritha 它必须工作。再次更新。现在重定向到哪里?
【解决方案2】:

通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteRule ^mydir1/([^/]+)/(.+?)-(\d+\.html)$ /$1/$2-1-$3 [L,R=301,NC]

【讨论】:

  • 谢谢。我不能在规则中使用“my-article”和“mydir2”这两个词,因为它在所有 URL 中都会有所不同。请查看相关的新变化。
  • 感谢您的回复。我试过了,但它没有在数字前的 url 中添加“-1”。即 my-article-first-86974.html 应更改为 my-article-first-1-86974.html。
  • 啊,我忽略了-1 的要求,抱歉再次编辑。
【解决方案3】:

您可以使用以下方法:

RewriteEngine On
RewriteRule ^/mydir2/(.*)+$ /mydir1/mydir2/$1

【讨论】:

  • 重定向my-article-1
  • 谢谢,我对我的问题做了一些修改。
【解决方案4】:

在你的 htacces 中试试这个:

<IfModule mod_rewrite.c>
        RewriteEngine   On
        RewriteBase http://mysite.com 
        RewriteRule     ^/(.+)$ /mydir1/mydir2/$1 [L]
</IfModule>

注意:此代码未经测试。

【讨论】:

    【解决方案5】:

    试试这个,

        # SEO URL Settings
        RewriteEngine On
        # If your installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 
    
        RewriteBase /
        RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
        RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
        RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
    

    你可以看到这个RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

    注意:代码未测试

    【讨论】:

    • 谢谢。我不能在规则中使用“my-article”和“mydir2”这两个词,因为它在所有 URL 中都会有所不同。请查看相关的新变化。
    • 谢谢,我试过你的回答,它影响了我项目中的所有网址,我只想重定向以mysite.com/mydir1开头的网址......
    猜你喜欢
    • 2011-03-23
    • 2013-02-05
    • 1970-01-01
    • 2012-02-03
    • 2015-12-23
    • 2015-07-28
    • 2013-12-21
    • 2015-03-04
    相关资源
    最近更新 更多