0

My htaccess is getting stuck in an infinite loop. I'm trying to strip the .html extension off a URL, if there is one, and then use the leftover string as a query string.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=302,L]

RewriteRule ^resources/blog$ /resources/blog/ [R=302,L,QSA]
RewriteRule ^resources/blog/$ resources/blog.php [L,QSA]
RewriteRule ^resources/blog/.*?([^\.\/]*)$ resources/blog.php?pname=$1 [L,QSA]

UPDATE:

Here's how it sit at this time. I've commented some stuff out while troubleshooting.

Options +SymLinksIfOwnerMatch -MultiViews

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

#Redirect to URL without .html extension (Pretty URL)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=302]

# Pretty url for Couch CMS blog
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^resources/blog/.*?([^\.\/]*)$ resources/blog.php?pname=$1 [L,QSA,END]

RewriteRule ^resources/blog$ /resources/blog/ [R=302,L,QSA]
RewriteRule ^resources/blog/$ resources/blog.php [L,QSA]

# RewriteRule ^resources/blog/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ resources/blog.php?d=$1$2$3 [L,QSA]
# RewriteRule ^resources/blog/[^\.]*?([^/\.]*)/$ resources/blog.php?fname=$1 [L,QSA]
# RewriteRule ^resources/blog/[^\.]*?([^/\.]*)$ "$0/" [R=302,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

</IfModule>

UPDATE 2

File structure is as follow:

/
|-- static_html_files/
|-- resources/
|----- blog.php
|----- newsletter.html
|-- .htaccess
|-- index.html

Most files are static and their URL's need to be 'prettified' by stripping the .html extension from them and then redirected to with the pretty URL.

The blog.php has to be prettified as well and the Couch CMS generated links need to be rewritten. The rule for rewriting the couch cms links is the one that keeps looping.

UPDATE 3

It seems there is a 301 redirect somewhere but I don't have a 301 in my htaccess? Not sure where to go from here or where to look for this redirect. See Redirect Check results below:

https://xxxxx.ca/resources/blog/yyyyy

HTTP/1.1 301 Moved Permanently
Date: Tue, 25 Jun 2019 20:58:31 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=poplcjkqcgd4gk57c1l3lcdjg7; path=/
Location: https://xxxxx.ca/resources/blog/yyyyy.html
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

https://xxxxx.ca/resources/blog/yyyyy.html

HTTP/1.1 302 Found
Date: Tue, 25 Jun 2019 20:58:31 GMT
Server: Apache
Location: https://xxxxx.ca/resources/blog/yyyyy
Content-Length: 263
Content-Type: text/html; charset=iso-8859-1

[RINSE & REPEAT AD NAUSEAM]

UPDATE 4

After some debugging, I seem to be having an Apache problem with mod_rewrite and 'add path info postfix' which is generally solved by specifying -MultiViews and/or adding the DPI flag to the rules but neither of these seem to solve any of my problems.

halfer
  • 19,824
  • 17
  • 99
  • 186
ReX357
  • 1,199
  • 3
  • 19
  • 43
  • "use the leftover string as a query string" - although that's not really what your regex is doing - but that doesn't explain the "infinite loop". Do you mean a rewrite loop? Or redirect loop? Please include examples of the URLs being requested, your filesystem structure and where this `.htaccess` file is located. Do you have a physical subdirectory called `/blog`, as well as a file called `/blog.php`? – MrWhite Jun 25 '19 at 13:46
  • @MrWhite It's a rewrite loop I believe. I think it just keeps meeting the condition for the last line. – ReX357 Jun 25 '19 at 15:03
  • And what about the URLs being requested and filesystem structure? What URLs are you linking to in your HTML. Do these `.html` URLs map to physical files of the same name? (It looks like this is the case from your directives, the `.html` extension isn't simply cosmetic.) – MrWhite Jun 25 '19 at 18:33
  • @MrWhite Bit more background. Website doesn't have a CMS, it's built in Webflow which just exports a set of html files and all the links in the code have .html extensions. So the one directive strips the HTML extension from the requested 'html' files. Then the directive at the bottom actually maps a filename with no extension to a physical .html file. Now the blog part is actually jimmy rigged in using Couch CMS which itself emits 'fake' html links with no physical html files but instead maps to the query string. I know this is super convoluted but I have to work with this for now. – ReX357 Jun 25 '19 at 20:12
  • @MrWhite I updated the post and added the directory structure. At this point, I just need to be able to stop the Rewrite engine from running after one pass on the CMS pretty url rule. – ReX357 Jun 25 '19 at 20:46
  • "...all the links in the code have .html extensions." - If that's the case then there is no point trying to remove the `.html` extension in `.htaccess`. – MrWhite Jun 25 '19 at 21:53

0 Answers0