Barry

htaccess redirects behaving strangely

Recommended Posts

I'm trying to direct all pages from their www and http versions to their non www https versions but I'm experiencing some really strange redirecting behaviour instead.

  • When I uncomment the http to https lines in the htaccess, I get a too many redirects error on my site
  • I thought that Processwire by default redirected from www to non www?

When I apply the following line after the Processwire directives

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

The redirecting seems to work fine but for some reason many of the pages redirect to the homepage.

I don't know if its a factor but I have jumplinks and multi-language installed and running.

I've removed all custom rules from the htaccess just so that people see the pages they click on from search engines, but I'm hoping this can be resolves quick. Ideas?

Share this post


Link to post
Share on other sites

maybe you have some other vhost rules active in your hosting settings? some hosting providers have settings to choose a www or non-www version of the site so if the vhost and your htaccess redirect to the opposite it would be an infinite loop.

  • Like 1

Share this post


Link to post
Share on other sites

I placed the following rules at the very top of the htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.thedjcompany\.nl [NC]
RewriteRule ^(.*)$ http://thedjcompany.nl/$1 [L,R=301]

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

also removing the RewriteEngine On which was lower down in the htaccess and now it all works

Share this post


Link to post
Share on other sites
31 minutes ago, bernhard said:

maybe you have some other vhost rules active in your hosting settings? some hosting providers have settings to choose a www or non-www version of the site so if the vhost and your htaccess redirect to the opposite it would be an infinite loop.

But I was using standard ProcessWire htaccess, so it should have all been working at ProcessWire intended

Share this post


Link to post
Share on other sites

I should mention that I have ProCache installed but not activated at the moment though it was on at some point to test the site. But since the site needed some thorough development, so save time clearing the cache, I simply cleared it and turned it off. I did notice some extra rules in the htaccess because of this. Could that have been the cause? 

Share this post


Link to post
Share on other sites
Just now, Peter Knight said:

I had that installed and it showed that Processwire was redirecting my page first tot a page with a get variable ?it= or something or other and then it would redirect to the homepage.

Share this post


Link to post
Share on other sites
1 minute ago, Barry said:

I had that installed and it showed that Processwire was redirecting my page first tot a page with a get variable ?it= or something or other and then it would redirect to the homepage.

Glad you go it sorted
Sounds like a caching issue. Even with ProCache turned off, I generally have to resave a page and clear the cache etc.

Share this post


Link to post
Share on other sites

Dunno if the problem I have had is exactly the same, but, FWIW, I have a site with ProCache and regardless of whether I delete and rebuild the cache, if I add a redirect in `.htaccess` to a page with an anchor tag, then the site actually ends up at a page with `?it=` and the redirect only gets as far as the page, the scroll to the anchor is ignored.

For me the fix was to use this syntax:

Redirect 301 /my-short-cut http://example.com/the/long/page/to/go-to/?it=qik/my-short-cut#theAnchorIwant

The rule I *would* have used until now was

Redirect 301 /my-short-cut http://example.com/the/long/page/to/go-to/#theAnchorIwant

but that fails as described.

Now I just amend to insert the `/?it=qik/` followed by the text to the right of `301` i.e. `my-short-cut` followed by a `/` and finally the `#anchor`.

This was a hard error I could NOT get rid of and I just had to accept not being able to do this. SO glad I found a workaround, hope this helps others 😊

PS: I don't think this is to do with ProCache, I think it's maybe to do with my hoster(?) - I'm not sure, but this one's been *such* a pain I really don't care, just glad to have got past it :)

Share this post


Link to post
Share on other sites

@alan, you might be better off using a mod_rewrite rule than using Redirect, because then you can use the [L] flag to avoid the other rewrite rules affecting the URL.

So instead of...

Redirect 301 /my-short-cut http://example.com/the/long/page/to/go-to/#theAnchorIwant

...try...

RewriteRule ^my-short-cut$ http://example.com/the/long/page/to/go-to/#theAnchorIwant [R=301,NE,L]

...just after...

RewriteEngine On

 

  • Like 4

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By dweeda
      I installed an SSL Certificate, then edited my .htaccess file:
        # -----------------------------------------------------------------------------------------------
        # 9. If you only want to allow HTTPS, uncomment the RewriteCond and RewriteRule lines below.
        # -----------------------------------------------------------------------------------------------
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      by uncommenting out the Rewrite lines.
      Now I get 404 error pages when I try go to any .../processwire-master/<pagename>/
      This includes my admin page at .../processwire-master/processwire/, so i can't get into my admin.
      What else do I need to do?
    • By pwfans
      Hello,
      1. Anybody can help, why this htaccess (location in root) doesn't work at 3.0.98 ? it show 404
      RewriteEngine on RewriteRule ^(.*)$ /subdirectory/$1 [L] That rule works in 3.0.62
      It redirect root request to subdirectory processwire installation and hiding the subdirectory name in url.
      I have try to uncomment htaccess inside subdirectory processwire installation :
      RewriteBase /pw/ to RewriteBase /subdirectory/ Doesn't help, still 404 when open the site from root domain.
      2. Is there anyway to find processwire version from processwire files ? not from admin login page.
      Thank you.
    • By rareyush
      I moved my site to Google cloud platform and i am stuck some where.
      my homepage is working fine but when it comes to other pages it says 404 not found
      and when i add this on virtual host 
       
      <Directory "/var/www"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> or <Directory "/var/www"> AllowOverride All </Directory>  
          I receive internal server error 
       
       
       
      I tried comment out this line to but still same thing 
      # RewriteBase /
      and
        # RewriteRule ^(.*)$ /index.php?it=$1 [L,QSA]
    • By joelplambeck
      Hi Guys,
      I'm trying to do my first migration to the customers existing server (IIS 10) . I ran the site as a subdirectory on my website for test purposes (everything works fine).
      Following the tutorial of Joss, I tryed the site on a local xampp server to make sure, it also works on a root directory. So far so good, everything works.
      Now I moved the files (from the xampp) to the customers server. The root/index page is shown but for every subpage i get 404 Errors...
      Hence I followed the troubleshooting guide for not working URLs:
      On the first sight, the .htaccess file is not recognized, therefore I contacted the host support. They said, it is recognized but not all modules are supported in the processwire .htaccess file. I did the "öalskjfdoal" test in the .htaccess file and didn't get a 500 Error.... BUT the rewrite rule from the hosts support, to proof the file is read, DID work... The support claims, they do not provide debugging... so basically the .htaccess file is recognized and working, but not throwing any errors (for whatever reason).
      Working rewrite rule (from support):
      RewriteEngine On RewriteBase / RewriteRule ^test\.asp$ index.html [NC,L] RewriteRule ^test\.html$ konzept.html [NC,L] RewriteRule ^test2\.html$ team.html [NC,L] The support said, a couple modules are not supported in the htaccess file, the supported ones are listed here: http://www.helicontech.com/ape/ (I think mod_rewrite is supported)
      As I do not completely understand what exactly is happening in the htaccess file, I'm stuck. I tried all suggestions I found regarding this topic on the forum, but none of them solved the problem.
       
      .htaccess.txt
    • By rolisx
      Hi Guys,
      Just finished a website locally and wanted to upload it on the webserver of my customer. I got a server 500 error. Now, the guidelines of the hoster (world4you.com) does not allow "Options" in the htaccess-file. So, when I uncomment these:
      Options -Indexes
      Options +FollowSymLinks
      the site is visible, but the content won't show and no links are available. Not sure if I need the Symlinks-part but I guess I need a workaround for the Index-part. Can anybody help here? I need the website up and running asap....
      Thanks!
      Roli