SOLVED Rewrite works in .htaccess but does not work in pre_main_global.conf

Jelf

Active Member
Jun 26, 2016
36
4
58
Redmond
cPanel Access Level
Root Administrator
I am trying to move rewrites from .htaccess files to
/etc/apache2/conf.d/includes/pre_main_global.conf

For testing purposes, the complete content of pre_main_global.conf is:

RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "https://domain.com" [R=301,L]

This rewrite works fine in an .htaccess file but does not work at all in pre_main_global.conf

The file permission for pre_main_global.conf is 644

The online tester at htaccess tester ♥ madewithlove does not show any syntax errors.

My main apache configuration file is /etc/apache2/conf/httpd.conf
That file says:
# Administrator locations for safely altering httpd.conf
Include "/etc/apache2/conf.d/includes/pre_main_global.conf"

I have root access. Using a terminal program I did:
apachectl -D DUMP_INCLUDES
One of the configuration files listed is:
/etc/apache2/conf.d/includes/pre_main_global.conf
This appears to indicate that the file pre_main_global.conf is not simply being ignored.

Any idea why my rewrite in pre_main_global.conf is not working?

I started a different thread asking how to get rewrite error messages in apache 2.4 but so far there is no answer in that thread.
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,497
1,968
363
cPanel Access Level
Root Administrator
Hey there! I'm not sure that pre_main_global is going to be the appropriate place for those type of redirects. You likely will need to use the individual vhost redirect area, which is at /etc/apache2/conf.d/userdata/ssl/2_4/username/domain.com/*.conf and /etc/apache2/conf.d/userdata/std/2_4/username/domain.com/*.conf. A global include is designed more for things that affect the entire Apache configuration, but I would expect redirects to need to be placed in individual files related to the user.

More details on that system can be found here: Modify Apache Virtual Hosts with Include Files | cPanel & WHM Documentation
 

Jelf

Active Member
Jun 26, 2016
36
4
58
Redmond
cPanel Access Level
Root Administrator
Thank you for pointing me in the right direction.

When the rewrite shown below is in an .htaccess file and I browse to domain.com/dummy then I am correctly redirected.

RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^dummy$ "http\:\/\/domain\.com\/" [R=301,L]

I followed the instructions in the documentation link you posted and made a virtual host conf file for the domain findmesar.com.
The only content of that conf file is the above rewrite.
I also disabled the .htaccess

I rebuilt httpd and restarted apache.

The terminal command apachectl -D DUMP_INCLUDES
confirms that my new conf file is included in apache

Alas, when I tested my virtual host conf file by browsing to findmesar.com/dummy
the redirect does not work.

I edited the main apache conf file by setting the log level to alert rewrite:trace6
I rebuilt httpd, restarted apache, cleared my browser cache.
Then a tried browsing to findmesar.com/dummy hoping that the main apache error.log file would have useful info.

Alas, the string “rewrite” does not appear in the main apache error.log file.
Below are a few snips from the error.log

Obviously I am missing something. Any suggestions?

[Wed Jun 22 12:26:22.374365 2022] [core:info] [pid 29045] [client 24.19.223.102:49902] AH00128: File does not exist: /home/username/public_html/domain/dummy
[Wed Jun 22 12:26:22.374814 2022] [cgi:error] [pid 29045] [client 24.19.223.102:49902] AH01228: exec used but not allowed in /home/mapping1/public_html/domain/404.shtml
[Wed Jun 22 12:26:22.452653 2022] [ssl:debug] [pid 29045] ssl_engine_kernel.c(422): [client 24.19.223.102:49902] AH02034: Subsequent (No.2) HTTPS request received for child 3 (server findmesar.propertylinemaps.com:443), referer: https://domain.com/dummy
[Wed Jun 22 12:26:22.453490 2022] [authz_core:debug] [pid 29045] mod_authz_core.c(844): [client 24.19.223.102:49902] AH01628: authorization result: granted (no directives), referer: https://domain.com/dummy
[Wed Jun 22 12:26:23.433624 2022] [ssl:debug] [pid 29045] ssl_engine_kernel.c(422): [client 24.19.223.102:49902] AH02034: Subsequent (No.3) HTTPS request received for child 3 (server host.domain.com:443)
[Wed Jun 22 12:26:23.434488 2022] [authz_core:debug] [pid 29045] mod_authz_core.c(844): [client 24.19.223.102:49902] AH01628: authorization result: granted (no directives)
[Wed Jun 22 12:26:28.440372 2022] [ssl:debug] [pid 29045] ssl_engine_io.c(1151): [client 24.19.223.102:49902] AH02001: Connection closed to child 3 with standard shutdown (server findmesar.propertylinemaps.com:443)
 
Last edited by a moderator:

Jelf

Active Member
Jun 26, 2016
36
4
58
Redmond
cPanel Access Level
Root Administrator
Solved.

The problem was a mistake in my RewriteRule
As a test I am trying to get domain.com/dummy to redirect to Website Domain Names, Online Stores & Hosting - Domain.com
The following rule does not work

RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^dummy$ "Website Domain Names, Online Stores & Hosting - Domain.com" [L,R=301,NC]

All of the following rules *do* work.

RewriteRule /dummy "Website Domain Names, Online Stores & Hosting - Domain.com" [L,R=301,NC]
RewriteRule /dummy "https\:\/\/domain\.com" [L,R=301,NC]
RewriteRule ^/dummy "https\:\/\/domain\.com" [L,R=301,NC]
RewriteRule ^/dummy$ "https\:\/\/domain\.com" [L,R=301,NC]
RewriteRule /dummy Website Domain Names, Online Stores & Hosting - Domain.com [L,R=301,NC]

The difference is the slash in front of dummy.

Tip: Here is how to use WHM to rebuild httpd and then restart apache.
Click: apache configuration > Global configuration
Scroll to the bottom and click Save
Scroll to the bottom and click Rebuild configuration and restart apache
 
Last edited by a moderator: