代码之家  ›  专栏  ›  技术社区  ›  Mittul At TechnoBrave

Htaccess 301博客重定向网址

  •  0
  • Mittul At TechnoBrave  · 技术社区  · 3 年前

    我在我的直播网站上做了301重定向,比如从这个网址 https://www.rosterelf.com/support-detail/1424/how-can-i-copy-shifts-from-a-day-to-another 指向此url https://www.rosterelf.com/support-detail/how-can-i-copy-shifts-from-a-day-to-another 它按照我的期望工作得很好。这是我的.htaccess代码,我是如何让它工作的。

    RewriteRule ^(support-detail)/\d+/([\w-]+)/?$  /$1/$2 [R=301,NC,L]
    

    现在,我想为我的博客添加一个我想重定向的条件。

    例如,我有这个网址 https://www.rosterelf.com/blog-detail/2211/how-to-create-a-winning-team 我想重定向到 https://www.rosterelf.com/blog/how-to-create-a-winning-team 因此,我写了下面的代码,但不幸的是,它对我不起作用。

    RewriteRule ^(blog-detail)/\d+/([\w-]+)/?$  /blog/$1/$2 [R=301,NC,L]
    

    这就是我的.htaccess文件的样子。

    <IfModule mod_rewrite.c>
    
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        ##
        ## You may need to uncomment the following line for some hosting environments,
        ## if you have installed to a subdirectory, enter the name here also.
        ##
        # RewriteBase /
    
        ##
        ## Uncomment following lines to force HTTPS.
        ##
        # RewriteCond %{HTTPS} off
        # RewriteRule (.*) https://%{SERVER_NAME}/$1 [L,R=301]
    
        ##
        ## Allow robots.txt
        ##
        RewriteRule ^robots.txt - [L]
    
        ## 301 redirect for old support details page url to new one 
        ## OLD URL https://www.rosterelf.com/support-detail/1903/how-can-employees-clock-inout-of-time-clock-different-slug 
        ## NEW URL https://www.rosterelf.com/support-detail/how-can-employees-clock-inout-of-time-clock-different-slug 
        
        RewriteRule ^(support-detail)/\d+/([\w-]+)/?$  /$1/$2 [R=301,NC,L]
        RewriteRule ^blog-detail/\d+/([\w-]+)/?$ /blog/$1 [R=301,NC,L]
    
        ##
        ## Black listed folders
        ##
        RewriteRule ^bootstrap/.* index.php [L,NC]
        RewriteRule ^config/.* index.php [L,NC]
        RewriteRule ^vendor/.* index.php [L,NC]
        RewriteRule ^storage/cms/.* index.php [L,NC]
        RewriteRule ^storage/logs/.* index.php [L,NC]
        RewriteRule ^storage/framework/.* index.php [L,NC]
        RewriteRule ^storage/temp/protected/.* index.php [L,NC]
        RewriteRule ^storage/app/uploads/protected/.* index.php [L,NC]
    
        ##
        ## White listed folders
        ##
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteCond %{REQUEST_FILENAME} !/.well-known/*
        RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/public/.*
        RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
        RewriteCond %{REQUEST_FILENAME} !/storage/app/resized/.*
        RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
        RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
        RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
        RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
        RewriteRule !^index.php index.php [L,NC]
    
        ##
        ## Block all PHP files, except index
        ##
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteCond %{REQUEST_FILENAME} \.php$
        RewriteRule !^index.php index.php [L,NC]
    
        ##
        ## Standard routes
        ##
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
    </IfModule>
    

    有人能指导我如何解决这个问题吗?

    谢谢

    0 回复  |  直到 3 年前
        1
  •  2
  •   RavinderSingh13 Nikita Bakshi    3 年前

    根据您展示的样品,您能否尝试以下操作。在htaccess文件中添加了一些修复程序以及新规则。

    请确保在测试URL之前清除浏览器缓存。

    <IfModule mod_rewrite.c>
    
    <IfModule mod_negotiation.c>
    Options -MultiViews
    </IfModule>
    
    RewriteEngine On
    
    ##
    ## You may need to uncomment the following line for some hosting environments,
    ## if you have installed to a subdirectory, enter the name here also.
    ##
    # RewriteBase /
    
    ##
    ## Uncomment following lines to force HTTPS.
    ##
    # RewriteCond %{HTTPS} off
    # RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [NE,L,R=301]
    
    ##
    ## Allow robots.txt
    ##
    RewriteRule ^robots.txt - [NC,L]
    
    ## 301 redirect for old support details page url to new one 
    ## OLD URL https://www.rosterelf.com/support-detail/1903/how-can-employees-clock-inout-of-time-clock-different-slug 
    ## NEW URL https://www.rosterelf.com/support-detail/how-can-employees-clock-inout-of-time-clock-different-slug 
    
    RewriteRule ^(support-detail)/\d+/([\w-]+)/?$  /$1/$2 [R=301,NC,L]
    RewriteRule ^blog-detail/\d+/([\w-]+)/?$ /blog/$1 [R=301,NC,L]
    
    
    ##
    ## Black listed folders
    ##
    RewriteRule ^(?:bootstrap|config|vendor|storage/(?:cms|logs|framework|temp/protected|app/uploads/protected))/ index.php [L,NC]
    
    
    ##
    ## White listed folders
    ##
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} !/.well-known/*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/public/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/app/resized/.*
    RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
    RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
    RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
    RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
    RewriteRule !^index\.php index.php [L,NC]
    
    ##
    ## Block all PHP files, except index
    ##
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} \.php$
    RewriteRule !^index\.php index.php [L,NC]
    
    ##
    ## Standard routes
    ##
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    
    </IfModule>
    
        2
  •  1
  •   anubhava    3 年前

    您可以有两个单独的重定向规则,如下所示:

    RewriteRule ^(support-detail)/\d+/([\w-]+)/?$ /$1/$2 [R=301,NC,L]
    
    RewriteRule ^blog-detail/\d+/([\w-]+)/?$ /blog/$1 [R=301,NC,L]
    

    确保完全清除浏览器缓存,并将这些规则放在.htaccess的顶部

    顺便说一句,你可以组合多个 Black listed folders 规则合一:

    ##
    ## Black listed folders
    ##
    RewriteRule ^(?:bootstrap|config|vendor|storage/(?:cms|logs|framework|temp/protected|app/uploads/protected))/ index.php [L,NC]