I ‘m trying to set a wildcard subdomain RewriteRule by combining some .htaccess rules I ‘d successfully implemented before, under the impression that I can use the same rules for my Apache vhosts file .
In essence, example.com and www.example.com go to the ancestor way and look for index.php. All early ( active ) subdomain requests at root level such as abc.example.com receive rewritten ( invisible to the browser ) to example.com/process.php?p=abc .
second, any requests for files from the subdomain outside of a base/root level need to be rewritten to be obtained from the root path of the standard domain without the subdomain. so abc.example.com/css/style.css needs to come from example.com/css/style.css
This is my attack at doing then. I get an Apache error :

You do n’t have permission to access /index.php on this waiter .

for any subdomain attack, early than www, which works as expected, ampere well as the criterion example.com, which still works all right .



        ServerAdmin webmaster@example.com
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined




        ServerAdmin webmaster@example.com
        ServerName other.example.com
        ServerAlias *.example.com
        DocumentRoot /var/www/example.com/public_html/

        
        Options FollowSymLinks
        AllowOverride all
        Require all granted

        RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
                RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
                RewriteRule ^$ /process.php?p=%2 [QSA,NC]
                RewriteCond %{REQUEST_FILENAME} !^$ [NC]
                RewriteRule ^(.*) http://www.example.com/$1 [P]
        

I ‘d based this off successfully redirecting a unlike domain ‘s root directory to example.com using this .htaccess file, which incorporates process.php and sends all early requests to the root of example.com.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Check the request isn't an existing file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.example.com/process.php [P]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [P]

And besides off this .htaccess test I ran to redirect a subdomain as a variable to process.php succesfully, though it did n’t catch other file requests, such as the cesium model above :

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ /process.php?p=%2 [QSA,NC]

Update 1

I ‘ve had ( apparently ) some success with using the following in place of my moment virtualhost entry. The process.php page is being output if I use a subdomain. While the blueprint matching seems to work in that regard, my actual process.php receives an evacuate variable, rather than the expect subdomain as a string ( for : process.php?p=%2 ) :


        ServerAdmin webmaster@example.com
        ServerName other.example.com
        ServerAlias *.example.com
        DocumentRoot /var/www/example.com/public_html

        
        Options FollowSymLinks
        AllowOverride all
        Require all granted
        RewriteEngine on
        RewriteBase /

        RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
        RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
        RewriteCond %{REQUEST_URI} ^/$
        RewriteRule ^$ /process.php?p=%2 [NC,QSA]
        

Update 2

I ‘ve solved this in a traffic circle way but I ‘m not certain if it ‘s the most elegant manner of doing then .
Using the above update 1 code, I saw that any question string was placid being appended but %2 was not being received by GET in my process.php page. I may have assumed incorrectly that although the url does n’t display the p=abc variable in the url to the browser, as per my code instructions, that the internal url distillery passed them on. Nonetheless, I realised that process.php could still determine the subdomain as a string using $_SERVER['HTTP_X_FORWARDED_HOST'] in PHP and use it as a variable on the page in a similar manner to my initial mind .

Leave a Reply

Your email address will not be published.