Redirect Rewrite Code
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:
[font="courier new">RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/%domain%\/%path%" [R=301,L]
But shouldn't this be:
[font="courier new">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.
-
What are you adding exactly? I.e., what is the redirect you're trying to add? 0 -
Well, I mean any redirect. [font="courier new">http://%domain% redirecting to [font="courier new">http://%domain%/%path% Or [font="courier new">[plain]http://example.tld[/plain] redirecting to [font="courier new">[plain]http://example.tld/dogs_and_cats_living_together/[/plain] 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: [font="courier new">http://%domain%/%path% 0 -
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 0 -
I should also note that I tested going directly to the path I was redirecting to: curl -vvI http://mydomain.wiki/redirect/index.php * About to connect() to mydomain.wiki port 80 (#0) * Trying ... * Connected to mydomain.wiki () 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 notdomain/path/file.php0 -
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 [plain]example.tld[/plain] and WordPress installed in a subdirectory from this DocumentRoot [plain]http://example.tld/path[/plain] then this issue rears it's ugly head. The code in the .htaccess file in the DocumentRoot for [plain]example.tld[/plain]: [font="courier new">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 [plain]http://example.tld/path[/plain]: [font="courier new">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: [font="courier new">[plain]curl -vvLI http://example.tld[/plain] will loop indefinitely. But if you change the code in the .htaccess file in the DocumentRoot for [plain]example.tld[/plain] to: [font="courier new">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 [font="courier new">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. 0 -
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. 0 -
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. 0 -
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. 0
Please sign in to leave a comment.
Comments
8 comments