sparek-3

Well-Known Member
Aug 10, 2002
2,138
260
388
cPanel Access Level
Root Administrator
Is the .htaccess code added to an account via the Redirect (cPanel -> Domains -> Redirects) page correct?

It looks like the code added to the .htaccess file is:

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/%domain%\/%path%" [R=301,L]


But shouldn't this be:

RewriteCond %{HTTP_HOST} ^.*$
RewriteCond %{REQUEST_URI} !^/%path%
RewriteRule ^/?$ "http\:\/\/%domain%\/%path%" [R=301,L]


Or something similar? The first one will just generate an infinite loop.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,138
260
388
cPanel Access Level
Root Administrator
Well, I mean any redirect.

http://%domain% redirecting to http://%domain%/%path%

Or

http://example.tld redirecting to http://example.tld/dogs_and_cats_living_together/

If you set that up in cPanel... doesn't it infinitely loop?

Type: Doesn't matter, 301 or 302 will do the same thing, 301 will just cache the redirect in your browser, you'll have to clear your browser cache to get it to stop.
Domain: Again, doesn't matter
/: Leave blank because we want /
Redirects To: http://%domain%/%path%
 

cPanelLauren

Product Owner II
Staff member
Nov 14, 2017
13,266
1,300
363
Houston
I don't have any issue with them looping which is why I was asking what you're putting. I tested this with a test domain on my server we'll call it mydomain.wiki

  • Type -- you're right doesn't matter - I used a 301
  • Domain: mydomain.wiki
  • /: nothing here because i want to redirect the root of the domain to a specific path
  • redirects to: http://mydomain.wiki/redirect/index.php
  • www. redirection: Redirect with or without www
  • Not a wildcard redirect

This puts the following in the .htaccess:
Code:
RewriteCond %{HTTP_HOST} ^mydomain\.wiki$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.wiki$
RewriteRule ^/?$ "http\:\/\/mydomain\.wiki\/redirect\/index\.php" [R=301,L]
I did a curl request to check and had it follow redirects:

Code:
$ curl -vvLI http://mydomain.wiki
* About to connect() to mydomain.wiki port 80 (#0)
*   Trying <MYIPADDRESS>...
* Connected to mydomain.wiki (<MYIPADDRESS>) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: mydomain.wiki
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Date: Thu, 02 Apr 2020 15:38:29 GMT
Date: Thu, 02 Apr 2020 15:38:29 GMT
< Server: Apache
Server: Apache
< Strict-Transport-Security: max-age=300; includeSubDomains; preload
Strict-Transport-Security: max-age=300; includeSubDomains; preload
< Location: http://mydomain.wiki/redirect/index.php
Location: http://mydomain.wiki/redirect/index.php
< Content-Type: text/html; charset=iso-8859-1
Content-Type: text/html; charset=iso-8859-1

<
* Connection #0 to host mydomain.wiki left intact
* Issue another request to this URL: 'http://mydomain.wiki/redirect/index.php'
* Found bundle for host mydomain.wiki: 0x6e0ec0
* Re-using existing connection! (#0) with host mydomain.wiki
* Connected to mydomain.wiki (<MYIPADDRESS>) port 80 (#0)
> HEAD /redirect/index.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: mydomain.wiki
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Thu, 02 Apr 2020 15:38:29 GMT
Date: Thu, 02 Apr 2020 15:38:29 GMT
< Server: Apache
Server: Apache
< Strict-Transport-Security: max-age=300; includeSubDomains; preload
Strict-Transport-Security: max-age=300; includeSubDomains; preload
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8

<
* Connection #0 to host mydomain.wiki left intact
 

cPanelLauren

Product Owner II
Staff member
Nov 14, 2017
13,266
1,300
363
Houston
I should also note that I tested going directly to the path I was redirecting to:
Code:
curl -vvI http://mydomain.wiki/redirect/index.php
* About to connect() to mydomain.wiki port 80 (#0)
*   Trying <MyIPAddress>...
* Connected to mydomain.wiki (<MyIPAddress>) port 80 (#0)
> HEAD /redirect/index.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: mydomain.wiki
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Thu, 02 Apr 2020 15:59:53 GMT
Date: Thu, 02 Apr 2020 15:59:53 GMT
< Server: Apache
Server: Apache
< Strict-Transport-Security: max-age=300; includeSubDomains; preload
Strict-Transport-Security: max-age=300; includeSubDomains; preload
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8

<
* Connection #0 to host mydomain.wiki left intact
Because you're redirecting just the root of the domain not domain/path/file.php
 

sparek-3

Well-Known Member
Aug 10, 2002
2,138
260
388
cPanel Access Level
Root Administrator
OK, finally had time to look at this a bit more indepth.


The issue isn't so much the cPanel redirect code. It has to do with the cPanel redirect code + WordPress's redirect code.


If you have a domain name example.tld and WordPress installed in a subdirectory from this DocumentRoot http://example.tld/path then this issue rears it's ugly head.

The code in the .htaccess file in the DocumentRoot for example.tld:

RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/example\.tld\/path" [R=301,L]


and the code in the .htaccess for the directory handling the WordPress script at http://example.tld/path:

RewriteEngine On
RewriteBase /path/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /path/index.php [L]


Now when you issue the command:

curl -vvLI http://example.tld

will loop indefinitely.

But if you change the code in the .htaccess file in the DocumentRoot for example.tld to:

RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteCond %{REQUEST_URI} !^/path
RewriteRule ^/?$ "http\:\/\/example\.tld\/path" [R=301,L]


Then this stops the indefinite loop.

Maybe there's a more eloquent solution. I'm not exactly sure what code is causing the issue. And it may very well be something within the code that WordPress add's to it's .htaccess file. I just know that by adding my RewriteCond %{REQUEST_URI} !^/path line into the cPanel generated .htaccess code - then this resolves the issue. I don't know what adding this line hurts, if anything, seems like it might be a good idea for cPanel to default to including this line.

This is my observations.
 
  • Like
Reactions: cPanelLauren

cPanelLauren

Product Owner II
Staff member
Nov 14, 2017
13,266
1,300
363
Houston
It's a valid point because so many use Wordpress or CMS systems that do something similar with redirects. I'll point it out to the teams involved in this change and see what their thoughts are and if it needs a case, to be honest, personally redirect rules hurt my head.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,138
260
388
cPanel Access Level
Root Administrator
Yea, definitely agree, mod_rewrite rules are painful to read/write and follow.

One key thing I've learned: always use lynx from the command line to test a new mod_rewrite rule. Because lynx isn't going to save any 301 redirect rules - there's just no browser cache. Or if I really want to see what is happening, I can use wget. I'm probably more wget than curl, but they can both kind of do the same thing.
 

cPanelLauren

Product Owner II
Staff member
Nov 14, 2017
13,266
1,300
363
Houston
Curl usually gets me what I need to know - you can set it to follow redirects too which is nice, I find I almost never use wget and I should use it more. Typically I'll run it on my test server that's on a separate network so I know it's not a local issue as well.