Forum Moderators: phranque

Message Too Old, No Replies

redirect help

mod rewrite .htaccess

         

garball

5:36 pm on Jan 15, 2011 (gmt 0)

10+ Year Member



My new website is loosing google ranking because I forgot about 301 redirects of old pages. I need help with the following in coding .htaccess:

page request: /gallery/v/everything_in_here redirect to /gallery
page request: /gallery3/everything_in_here_including_subdirectories to /gallery

The original request are for specifi images that don't exist however I want the redirect to be a directory! Is this possible? I'm getting 404 errors because /gallery/v/stuff.img and /gallery3/stuff/image.img doesn't exist and I need it to be re-directed to /gallery/ instead.

Thanks for any help.
(PS: I've spent days reading manuals, searching etc... can't seem to make this work)

g1smd

6:29 pm on Jan 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What have you tried so far?

Let's see the code, and a description of what it did compared to what you wanted

garball

7:51 pm on Jan 15, 2011 (gmt 0)

10+ Year Member



Sure,

There are a couple things going on here. I'm using word press so WP has inserted some code as noted. Everything after the WP entries are my manual entries which seem to be working. I also installed a re-direction module in WP (top of .htaccess) to experiment with the same thing. None the less, I still don't know how to re-direct the example given in the orignal post (maybe because it's a wild card issue, don't know). The 'uncommented' line is what I tried but didn't work.

Thanks,
Gary.


aspenridge:www.aspenridge.ca gary$ cat .htaccess
# Created by Redirection Module: Apache
# Sat, 15 Jan 2011 01:01:19 +0000
# Redirection 2.2.3 - [urbangiraffe.com...]

<Files .htaccess,.svn>
order allow,deny
deny from all
</Files>

php_value error_reporting E_ALL

Options +FollowSymlinks

<IfModule mod_rewrite.c>
</IfModule>

# End of Redirection

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>

# END WordPress
#RewriteRule ^gallery/v(/)?$ /gallery/
redirect permanent /index.html [aspenridge.ca...]
redirect permanent /media.htm [aspenridge.ca...]
redirect permanent /contact-us [aspenridge.ca...]
redirect permanent /marine.htm [aspenridge.ca...]
redirect permanent "/current projects.htm" [aspenridge.ca...]
redirect permanent "/marine kitchen.htm" [aspenridge.ca...]
redirect permanent /main.htm [aspenridge.ca...]
redirect permanent /kitchen&bath.htm [aspenridge.ca...]
redirect permanent /april.htm [aspenridge.ca...]
redirect permanent /april1.htm [aspenridge.ca...]
redirect permanent /testimonials [aspenridge.ca...]
redirect permanent /about-us.htm [aspenridge.ca...]
redirect permanent /conatact [aspenridge.ca...]
redirect permanent /waterst.htm [aspenridge.ca...]
redirect permanent /winslow.htm [aspenridge.ca...]
redirect permanent /gallery-2 [aspenridge.ca...]

end of .htaccess.

g1smd

10:03 pm on Jan 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you have used RewriteRule for any of your rules, you should use it for all of the rules. That is, don't mix both Redirect and RewriteRule within the same site, you can't guarantee the order they are processed in.

Additionally, list all of the redirects before any of the internal rewrites and make sure all of the rules use the [L] flag.

garball

3:36 am on Jan 16, 2011 (gmt 0)

10+ Year Member



Ok but that really does't answer my question. Anybody else?

jdMorgan

5:09 pm on Jan 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A slight tweak of your rule should work.

RewriteRule ^gallery(3|/v)/ /gallery/ [L]

but only if you place it *above* the WP code.

In addition, you should take heed of gismd's advice regarding mixing mod_alias and mod_rewrite directives.

The following threads in our Apache Library may be useful to you regarding the correct operation and performance of your server:

  • The Correct Order For Efficient htaccess [webmasterworld.com]
  • Reducing Costly Rewrites in Wordpress - Improving WordPress blog performance [webmasterworld.com]

    The information in the first may save you from a search engine indexing disaster, while that in the second may markedly improve your server's response time, making both Google and your users happier, or even stave off an early and unnecessary server upgrade.

    Jim
  • garball

    12:39 am on Jan 17, 2011 (gmt 0)

    10+ Year Member



    Hi Jim,

    Tried the above code, didn't work. Will also look at gismd's advice.

    Gary.

    jdMorgan

    9:40 pm on Jan 19, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Be sure that both
     Options +FollowSymlinks 

    and
     RewriteEngine on 

    are moved above all other mod_rewrite directives.

    Jim

    garball

    7:44 pm on Jan 20, 2011 (gmt 0)

    10+ Year Member



    They are. Furthermore, when I use the 'rewrite rule' instead of 'redirect permanent', not of the redirections work anymore. Is the syntax incorrect?
    Gary

    Beginning of .htaccess

    # Created by Redirection Module: Apache
    # Sat, 15 Jan 2011 01:01:19 +0000
    # Redirection 2.2.3 - [urbangiraffe.com...]

    <Files .htaccess,.svn>
    order allow,deny
    deny from all
    </Files>

    php_value error_reporting E_ALL

    Options +FollowSymlinks

    <IfModule mod_rewrite.c>
    </IfModule>

    # End of Redirection

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress
    RewriteRule ^gallery3/.*$ gallery/ [L,NC,R=301]
    RewriteRule ^gallery/v/.*$ gallery/ [L,NC,R=301]
    RewriteRule /index.html [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /media.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /contact-us [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /marine.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule "/current projects.htm" [aspenridge.ca...] [L,NC,R=301]
    RewriteRule "/marine kitchen.htm" [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /main.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /kitchen&bath.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /april.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /april1.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /testimonials [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /about-us.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /conatact.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /waterst.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /winslow.htm [aspenridge.ca...] [L,NC,R=301]
    RewriteRule /gallery-2 [aspenridge.ca...] [L,NC,R=301]

    g1smd

    7:48 pm on Jan 20, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    RewriteRule cannot see the leading slash of the URL requests. You might also find the code more reliable if you add the ^ "begins with" notation.

    Additionally, you must list all of the external redirects before any of the internal rewrites.

    The target URL for all external "301" redirects should include the protocol and domain name too.

    garball

    8:08 pm on Jan 20, 2011 (gmt 0)

    10+ Year Member



    Can you give me one example?

    Thanks
    Gary

    jdMorgan

    9:33 pm on Jan 24, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    One example, which handles eight of your old URLs:

    RewriteRule ^(marine(\ kitchen)?|current\ projects|april1?|kitchen&bath|waterst|winslow)\.htm$ http://www.aspenridge.ca/gallery [NC,R=301,L]

    *All* rewriterules which implement external redirects (i.e. those that include protocol and domain in the rule's substitution, or include an "[R]" flag) must precede any rewriterules which implement internal rewrites (e.g. your WP rewrite).

    Also, the directives

    Options +FollowSymlinks
    #
    RewriteEngine On
    RewriteBase /

    Should appear only once -- at the top, and before any other mod_rewrite directives.

    I strongly suggest that you delete the <IfModule> containers, unless you specifically desire this code to fail silently on a server where mod_rewrite is not installed or available... The only people who would likely want this behaviour is the WP script authors and hosting companies -- to reduce their own support costs... :(

    Jim

    garball

    9:07 am on Jan 25, 2011 (gmt 0)

    10+ Year Member



    Thanks but still no further ahead. Back to my original post:

    'page request: /gallery/v/everything_in_here redirect to /gallery' as an example.

    I simply need any request for non existent files (/gallery/v/who_knows_what) to be redirected to /gallery, that's it.

    Cheers,
    Gary.

    g1smd

    10:08 am on Jan 25, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Fix all of the stuff previously mentioned, then we can proceed knowing that none of that is causing a problem.

    garball

    6:26 pm on Jan 25, 2011 (gmt 0)

    10+ Year Member



    This is how it looks so far:


    # Created by Redirection Module: Apache
    # Sat, 15 Jan 2011 01:01:19 +0000
    # Redirection 2.2.3 - [urbangiraffe.com...]
    <Files .htaccess,.svn>
    order allow,deny
    deny from all
    </Files>
    php_value error_reporting E_ALL
    Options +FollowSymlinks
    # End of Redirection

    # BEGIN WordPress
    #<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    #</IfModule>

    # END WordPress
    #RewriteRule ^gallery3/.*$ gallery/ [L,NC,R=301]
    #RewriteRule ^gallery/v/.*$ gallery/ [L,NC,R=301]
    RewriteRule ^(marine(\ kitchen)?|current\ projects|april1?|kitchen&bath|waterst|winslow)\.htm$ [aspenridge.ca...] [NC,R=301,L]

    Gary,

    g1smd

    7:56 pm on Jan 25, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    You must list all external redirects before the internal rewrite, otherwise the external redirects will expose the internally rewritten filepath back out on to the web.

    Delete the opening and closing ifModule tags, they are not required.

    The external redirects should contain the protocol and domain name in the redirection target.

    You'll also need to add the standard non-www to www redirect as the last of the redirects.

    garball

    8:42 pm on Jan 25, 2011 (gmt 0)

    10+ Year Member



    Sorry, I can't follow you here. I don't have enough understanding of any of this to understand what you're saying (no offence to you). I'll give up and live with this.

    Thank you for your help.

    Gary,

    jdMorgan

    8:07 pm on Jan 31, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You can give up, but your site cannot "live" with this -- You will likely have ranking problems forever until it is corrected... It's important not to disregard the fact that in addition to affecting the operation of your server, external redirection and internal rewriting affect your search engine indexing and ranking as well... This is not some "just little script" here, it is server configuration code.

    Also, I should note that your chance of success using code that you don't understand is essentially zero. See the references cited in our Apache Forum Charter, and spend a few hours studying those documents. If you are in a big hurry because your site is important to you, then spend a couple of days studying those documents instead...

    There are also some worthwhile introductory threads in our Apache Forum Library.

    Having spent some time on the basics, the functions and improvements in this code should be evident:

    # Created by Redirection Module: Apache
    # Sat, 15 Jan 2011 01:01:19 +0000
    # Redirection 2.2.3 - [urbangiraffe.com...]
    <Files .htaccess,.svn>
    Order allow,deny
    Deny from all
    </Files>
    #
    php_value error_reporting E_ALL
    #
    Options +FollowSymlinks
    # End of Redirection Module code
    #
    RewriteEngine on
    RewriteBase /
    #
    # Externally redirect old URLs to "/gallery/" page
    RewriteRule ^(gallery(/v|3)/|marine(\ kitchen)?|current\ projects|april1?|kitchen&bath|waterst|winslow)\.htm$ http://www.aspenridge.ca/gallery/ [NC,R=301,L]
    #
    # Externally redirect requests for non-blank non-canonical hostnames to canonical hostname
    RewriteRule %{HTTP_HOST} !^(www\.aspenridge\.ca)?$
    RewriteRule ^(.*)$ http://www.aspenridge.ca/$1 [R=301,L]
    #
    # Internally rewrite URLs which do not resolve to physically-existing files or directories to
    # WP script, except for URLs which resolve to filetypes which WP cannot and should not generate
    # BEGIN modified WordPress rewrite
    RewriteCond $1 !(\.(gif|jpe?g|png|ico|js|css|xml|txt)|^index\.php)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /index.php [L]
    # END modified WordPress rewrite

    Jim

    garball

    8:26 pm on Jan 31, 2011 (gmt 0)

    10+ Year Member



    Jim,

    I agree with you. I have spent many many hours reading the apache 'rewrite' and other modules documentation but it's simply too complicated for me to understand. What I did in the interm was to use a re-direction module for WordPress, essentially a 'GUI' within WP that has helped. None the less, I am going to study your code to help me better understand who rewriterule works. Just as a BTW, here is the output of what the WP module created:


    # Created by Redirection Module: Apache
    # Tue, 25 Jan 2011 22:43:19 +0000
    # Redirection 2.2.3 - [urbangiraffe.com...]

    <Files .htaccess,.svn>
    order allow,deny
    deny from all
    </Files>

    php_value error_reporting E_ALL

    Options +FollowSymlinks

    <IfModule mod_rewrite.c>
    RewriteRule gallery/v/(.*)\.(.*) /gallery/ [R=301,L]
    RewriteRule files/(.*)\.(.*) /gallery [R=301,L]
    RewriteRule ^index\.html$ /index.php [R=301,L]
    RewriteRule ^media$ /media-2 [R=301,L]
    RewriteRule ^conatact\.htm$ /contact [R=301,L]
    RewriteRule marine(.*)\.htm /gallery [R=301,L]
    RewriteRule ^current%20projects\.htm$ /gallery [R=301,L]
    RewriteRule ^main\.htm$ /index.php [R=301,L]
    RewriteRule ^kitchen%26bath\.htm$ /gallery [R=301,L]
    RewriteRule april(.*)\.htm /gallery [R=301,L]
    RewriteRule ^testimonials$ /media-2 [R=301,L]
    RewriteRule ^about-us$ /about [R=301,L]
    RewriteRule ^waterst\.htm$ /gallery [R=301,L]
    RewriteRule ^winslow\.htm$ /gallery [R=301,L]
    RewriteRule ^gallery-2$ /gallery [R=301,L]
    RewriteRule ^contact-us$ /contact [R=301,L]
    </IfModule>

    # End of Redirection

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    I am going to tryout your script too.

    Cheers,
    Gary,

    g1smd

    8:36 pm on Jan 31, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Please dump or improve that new code as it contains some of the most horrendous junk possible.

    It had dozens of very serious flaws, not limited to every URL request causing multiple thousands of "trial matches" within the RegEx patterns, and every request triggering multiple unnecessary disk reads, which will cause the site to run slowly as well as causing the server hard disk to fail early.

    garball

    10:11 pm on Jan 31, 2011 (gmt 0)

    10+ Year Member



    Humm,

    That's too bad. These plugins are supposed to make life easier and circumvent manually coding the .htaccess file (here's the plugin site FYI: [urbangiraffe.com...] I'll substitute Jim's code and see what happens. Maybe I'll learn something here. Thanks for the insight.

    Gary.

    g1smd

    11:36 pm on Jan 31, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    I can guarantee that Jim's code will process many orders of magnitude quicker.

    However, do report any further issues, as it is not entirely unknown for typos to creep in with complex code such as this.

    garball

    2:00 am on Feb 2, 2011 (gmt 0)

    10+ Year Member



    Ok, tried Jims' code and couldn't open my site. Got the following error as soon as I tried going to the main page:

    Safari can’t open the page.
    Too many redirects occurred trying to open “http://www.aspenridge.ca/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

    Gary,.

    g1smd

    9:57 am on Feb 2, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Likely a simple typo that Jim will easily spot coming back to the code "fresh".

    You can try to diagnose the problem by using "Live HTTP Headers" for Firefox and examining the headers returned.

    jdMorgan

    8:00 pm on Feb 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Don't see a problem, but then, I don't know exactly what code was being tested...

    Possible cause: The protocol and domain name are missing from the substitution in most of your redirect rules (those ending in [R=301,L]) posted above.

    Jim