1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

.htaccess redirection help

Discussion in 'Server Configuration and Hosting' started by imthebest, Aug 22, 2014.

  1. imthebest

    imthebest Formerly Super120

    Hi guys,

    I need help to redirect everything from:

    Code:
    http://www.mysite.com/forums/tags.php
    http://www.mysite.com/forums/tags.php?tag=videos
    http://www.mysite.com/forums/tags.php?tag=videos&page=2
    http://www.mysite.com/forums/tags.php?tag=funny+stuff
    http://www.mysite.com/forums/tags.php?tag=funny+stuff&page=2
    to:

    Code:
    http://www.mysite.com/forums/tags/
    http://www.mysite.com/forums/tags/videos/
    http://www.mysite.com/forums/tags/videos/page-2
    http://www.mysite.com/forums/tags/funny+stuff/
    http://www.mysite.com/forums/tags/funny+stuff/page-2
    How I can do this?

    Thanks!
     
  2. melbo

    melbo Well-Known Member

    In .htaccess or your httpd conf file (whichever you use)

    Code:
    redirect 301 /forums/tags.php http://www.mysite.com/forums/tags/
    redirect 301 /forums/tags.php?tag=videos http://www.mysite.com/forums/tags/videos/
    etc.
     
  3. imthebest

    imthebest Formerly Super120

    Thanks for the input but I forgot to mention that those URLs are just examples... I want to redirect a lot of those and I showed the URL structure looking for a redirection rule that will work for all of these!
     
  4. melbo

    melbo Well-Known Member

    @Jake Bunce is the wizard...

    I use this to wildcard redirect all urls that match the criteria (/forum/blogs/) to a single url. you might be able to adapt on it or at least make it so you only need one rule for every 5 urls?

    Code:
    RedirectMatch 301 ^/forum/blogs/.*$ https://www.mysite.com/forums/blogs-have-moved.117/
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    There are 3 patterns there.

    Add these rules to the top of the .htaccess file in the web root of http://www.mysite.com :

    Code:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)&page=([0-9]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/page-%3? [R=301,L]
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/? [R=301,L]
    
    RewriteRule ^tags\.php$ /forums/tags/? [R=301,L]
    
     
    eberkund, melbo and imthebest like this.
  6. melbo

    melbo Well-Known Member

    imthebest likes this.
  7. imthebest

    imthebest Formerly Super120

    @Jake Bunce unfortunately it's not working... for example this link:

    www.mysite.com/forums/tags.php?tag=neutralidad

    Is still going to the same path and XenForo is displaying the standard error message: "The request page wasn't found".

    This is my .htaccess file on the root of my site:

    Code:
    ###########################################################
    # mod_rewrite and disable indexes
    ###########################################################
    RewriteEngine on
    Options +FollowSymLinks
    Options -Indexes
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)&page=([0-9]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/page-%3? [R=301,L]
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/? [R=301,L]
    
    RewriteRule ^tags\.php$ /forums/tags/? [R=301,L]
    
    ###########################################################
    # Add 'www.' prefix
    ###########################################################
    RewriteCond %{HTTP_HOST} ^mysite.com [NC]
    RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
    
    ###########################################################
    # Root to forums
    ###########################################################
    RedirectMatch permanent ^/$ http://www.mysite.com/forums
     
  8. imthebest

    imthebest Formerly Super120

    @Mike could you please provide some assistance here? Many thanks!
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    My mistake.

    Add these rules to the top of the .htaccess file in the /forums directory:

    Code:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)&page=([0-9]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/page-%3? [R=301,L]
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%2/? [R=301,L]
    
    RewriteRule ^tags\.php$ /forums/tags/? [R=301,L]
    
     
    melbo and imthebest like this.
  10. imthebest

    imthebest Formerly Super120

    @Jake Bunce I see that your redirection scripts are having troubles with these URLs:

    tags.php?tag=espa%F1ol
    tags.php?tag=telefon%EDa+m%F3vil
    tags.php?tag=videoc%E1mara

    Currently it's redirecting, respectively, to:

    tags/espa%25F1ol/
    tags/telefon%25EDa+m%25F3vil/
    tags/videoc%25E1mara/

    It should redirect, respectively, to:

    tags/español/
    tags/telefonía+móvil/
    tags/videocámara/

    Thanks!!
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Try this:

    Code:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)&page=([0-9]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/${unesc:%2}/page-%3? [R=301,L]
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/${unesc:%2}/? [R=301,L]
    
    RewriteRule ^tags\.php$ /forums/tags/? [R=301,L]
    
    I haven't tried this before but that's what I found in this guide:

    http://stackoverflow.com/questions/18990593/apache-rewriterule-redirection-with-url-encoded
     
  12. imthebest

    imthebest Formerly Super120

    Thanks @Jake Bunce but unfortunately it's not working.

    :(
     
  13. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Then maybe this syntax:

    Code:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)&page=([0-9]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%{unesc:%2}/page-%3? [R=301,L]
    
    RewriteCond %{QUERY_STRING} (^|\?)tag=([^&]+)($|&)
    RewriteRule ^tags\.php$ /forums/tags/%{unesc:%2}/? [R=301,L]
    
    RewriteRule ^tags\.php$ /forums/tags/? [R=301,L]
    
     
  14. imthebest

    imthebest Formerly Super120

    @Jake Bunce it is still not working. I even tried adding the "NE" flag as per your source but no luck.

    Any idea please?
     

Share This Page