Jump to content

htaccess redirects behaving strangely


Barry
 Share

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?

Link to comment
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
Link to comment
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

Link to comment
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

Link to comment
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? 

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 1 month later...

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 :)

Link to comment
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By wwwouter
      Some context: I want to use PHP variables in my CSS (more info below) and found a solution on CSS-tricks that looks fairly elegant and somewhat solid to me. It's pretty simple, I created a file style.css.php inside the site/templates/ directory and load that in my page head. In style.css.php is the following:
      <?php header("Content-type: text/css; charset: UTF-8"); header("Charset:utf-8"); if ($homepage->hero_image) { echo <<<CSS .hero { background: url($homepage->hero_image->url) no-repeat; } CSS; } ?> Because of the following RewriteCond (line 373) in the htaccess file the server sends a 403 error back when the file is requested:
      # Block access to any PHP or markup files in /site/templates/ or /site-*/templates/ RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/templates($|/|/.*\.(php|html?|tpl|inc))($|/) [NC,OR] (My htaccess file is @version 3.0 and @htaccessVersion 301)
      This is how I thought I could fix that (based on these answers on stack overflow) but it does not work:
      # Block access to any PHP or markup files in /site/templates/ or /site-*/templates/ RewriteCond %{REQUEST_URI} (^|/)(site|site-[^/]+)/templates($|/|/((?!style\.css).)*\.(php|html?|tpl|inc))($|/) [NC,OR] I tested the rule with htacess tester and htaccess check and both worked for me, but on my site I still get a 403 instead of the file.
      I'm working on localhost, using MAMP (not sure if that's relevant).
      A bit more about what I want to do achieve specifically:
      I want to use an image as a background-image for an element, not place it as an image. This image is provided by the user via a field and can therefore change.
      I know I can achieve this like this:
      echo "<section class='hero' style='background-image: url($page->hero_image->url)'></section>"; But I would prefer a method other than inlining because of scalability and cleanliness. (I admit the extra link in the page head is not ideal either)
       
      P.s. this is my first post here, I hope it's submitted in the right forum and my explanation is clear.
    • By RDC
      Is the right way to get Processwire to skip a folder (ie not process it as its running a different CMS) to put
      RewriteCond %{REQUEST_URI} !^/shop/.*$
      just before 
      RewriteRule ^(.*)$ index.php?it=$1 [L,QSA]
      in .htaccess?
    • By Spiria
      I am writing here instead of the ProCache forum because I have the impression that several nginx experts could solve this. In its new version, Procache proposes new .htaccess rules that are difficult to translate into nginx rules. We currently have a problem with the trailing slash of URLs which gives us a bad score in SEO analysis tools such as semrush.com. 
      Indeed, a canonical URL is either without or with "/", but cannot be served by both options. Although ProcessWire without ProCache respects this rule, ProCache has no difficulty serving both, which is considered an SEO optimisation fault. I believe that ProCache version 2 solves this problem with these new rules, but I can't understand the following. Does anyone have any idea how to translate this into a nginx rule?
      Attached also the whole proposed .htaccess for ProcessWire.
      # PROCACHE v2/31885be14d6cfb4b2b0d3e533260bded -------------------------------------------------- RewriteCond %{REQUEST_METHOD} !=POST RewriteCond %{QUERY_STRING} !.*=.* RewriteRule ^.*/$ - [E=pwpcstep:pour,E=pwpcname:index] RewriteCond %{ENV:pwpcstep} "=pour" RewriteCond %{HTTP_COOKIE} !^.*(wire_challenge|wires_challenge).*$ RewriteRule ^.*/$ - [E=pwpcstep:stir,E=pwpcpath:%{DOCUMENT_ROOT}/site/assets/ProCache-00478359c5e65dbada1075bfbd4] RewriteCond %{ENV:pwpcstep} "=stir" RewriteCond %{ENV:pwpcpath}/$0/%{ENV:pwpcname}.html -f RewriteRule ^.*$ - [E=pwpcstep:drink,E=pwpcfile:$0/%{ENV:pwpcname}.html] RewriteCond %{ENV:pwpcstep} "=drink" RewriteRule ^(.*) %{ENV:pwpcpath}/%{ENV:pwpcfile} [L] <ifModule mod_headers.c> Header set X-PWPC "ProCache" env=pwpcstep Header set X-Powered-By "ProcessWire CMS, ProCache" env=pwpcstep </ifModule> RewriteCond %{REQUEST_URI} "^/~?[-_.a-zA-Z0-9/]*$" # /PROCACHE -------------------------------------------------------------------------------------  
      .htaccess-procache
    • By Peter Knight
      Can anyone tell me which .htaccess directive might be blocking a site import feature from scanning my public_html folder and listing all files and folders.
      These would be both PW files/folders but also some non PW folders which my PW site references for media.
      Latest .htaccess file on Github
      I can see various options from Section 15 onwards (# 15. Access Restrictions (v301)) but nothing that would block both SSH and FTP scans my VPS site import file uses.
      When I point the same site import tool at a non PW site on the same directory it works.
      I could disable htaccess temporarily but it's a busy and active site.
      Thanks
      P

       
       
    • By daniel_puehringer
      Hi Community,

      Sadly I cannot intall ProcessWire on my webserver (Hosted at https://www.world4you.com/de/startseite.html, a small hosting provider in Austria)
      Here is the Problem: 
      1. I downloaded the current masterbranch as a .zip file (After a few tries I also downloaded the code via the git clone command, that led to the same error), unzipped it and transferred it to my server. Since there is already a wordpress site installed on the root directory, I transferred the processwire files into /relaunch and created a subdomain.
      2. I tried to install the language starter kit. As soon as I navigate to that folder Screenshot1 (see attached file 'processWireBug1') is shown. Everything looks nice, except the red text referring to the 'mod_rewrite' rule. The webserver provides the 'apache mod_rewrite' rule.
      3. As soon as I want to press 'Check again' or 'Continue to next step' an error Page with 500 error (see Screenshot 'processWireBug2') occurs.
      4. You can find my .htaccess file attached to this post..htaccess

      How could I solve this issue?

      Some additional information:
      1) the webserver provides the apache mod_rewrite (I checked that several times) and has php version 7.3. installed.
      2) since there is a wordpress site installed on the root directory, there is also a .htaccess file on the root directory. Could this .htaccess file have an impact on the .htaccess file for my processwire website?

      Thanks for your help and greetings from Austria,
      Daniel


×
×
  • Create New...