• Hi everyone,

    I just migrated a site to WordPress and there are many pages that don’t have the same URL anymore. What I want to do is redirect all non-existent pages to my homepage.

    I’ve looked and researched a lot and found that this could be done through the .htaccess file.

    Basically I created a blank notepad file, posted the code below, saved it as 1.htaccess(.txt) and uploaded to the public html folder. I also put the permissions to 666.

    Why is this not working?

    Below is the code I used:

    RewriteEngine On
    RewriteBase /
    
    #if the query string has an a parameter
    RewriteCond %{QUERY_STRING} (^|&)a= [NC]
    #Redirect and remove query string parameters
    RewriteRule .*  https://www.sakokasa.com/? [R=301,L]
    
    #if the query string has a pagewanted parameter
    RewriteCond %{QUERY_STRING} (^|&)pagewanted=all [NC]
    #Redirect and remove query string parameters for html pages
    RewriteRule (.+\.html)  https://www.sakokasa.com/$1? [R=301,L]
Viewing 7 replies - 1 through 7 (of 7 total)
  • The file should just be named .htaccess

    Make sure it is in your wordpress site installations root directory.

    Thread Starter TeachMe

    (@teachme)

    Hey Josh,

    It is named correctly .htaccess

    I saved it under public_html folder just like I saw in the youtube video. How do I know where is my installations root directory?

    The file name should be just .htaccess as you say.

    The root directory is the same directory where you put the WordPress files when you set up your site. So in the public_html folder, if you can also see three folders called wp-admin, wp-content and wp-includes then it is in the right spot

    Thread Starter TeachMe

    (@teachme)

    Thanks Simon,

    I found my root! but somehow the previous code still did not work. It gave me a page with 404 error, and suggested the user to click on a few of my pages instead.

    I also tried using this

    ErrorDocument 404 /index.php

    and it didn’t work either

    There are all kinds of errors with those rules in that .htaccess.

    Are you trying to use a literal ^ ?

    If so, it needs to be escaped:

    RewriteCond %{QUERY_STRING} (\^|&)a= [NC]

    Also what are the question marks for at the end of those targets in the RewriteRules?
    I would leave them off.

    RewriteRule .* https://www.sakokasa.com/ [R=301,L]

    RewriteRule (.+\.html) https://www.sakokasa.com/$1 [R=301,L]

    Thread Starter TeachMe

    (@teachme)

    Hi Mickey,

    I change my .htaccess file and put this after reading what you wrote:

    RewriteEngine On
    RewriteBase /
    
    #if the query string has an a parameter
    RewriteCond %{QUERY_STRING} (\^|&)a= [NC]
    #Redirect and remove query string parameters
    RewriteRule .* https://www.sakokasa.com/ [R=301,L]
    
    #if the query string has a pagewanted parameter
    RewriteCond %{QUERY_STRING} (\^|&)apagewanted=all [NC]
    #Redirect and remove query string parameters for html pages
    RewriteRule (.+\.html) https://www.sakokasa.com/$1 [R=301,L]

    somehow it doesn’t redirect me to the home page. the file is in my root access because i can see the wp-admin and wp-content folder. Is there a special encoding I need to use? right now it is using UTF-8

    I need a bit more info. What exactly are you trying to redirect to your home page. I’m not sure why a string would have a literal ^ in it, but I don’t know your set up. (Note: the character ^ means ‘begins with’ when not escaped, so are you trying to state that the query string begins with something or do you actually have the literal character ^ in your strings? So according to the first rule:

    If the query string is ^a= or &a= redirect it to https://www.sakokasa.com/

    The second rule says:
    If the query string is ^apagewanted=all or &apagewanted=all redirect the blahblah.html file to https://www.sakokasa.com/$1

    Maybe this is what you’re trying to do (noticed the usage of begins with ‘^’ and I added $1 in the end of the first rewriterule target):

    #if the query string has an a parameter
    RewriteCond %{QUERY_STRING} ^&a= [NC]
    #Redirect and remove query string parameters
    RewriteRule .* https://www.sakokasa.com/$1 [R=301,L]
    
    #if the query string has a pagewanted parameter
    RewriteCond %{QUERY_STRING} ^&apagewanted=all [NC]
    #Redirect and remove query string parameters for html pages
    RewriteRule (.+\.html) https://www.sakokasa.com/$1 [R=301,L]
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘htaccess and redirects not working’ is closed to new replies.