2
\$\begingroup\$

After 8 years with the same Apache webserver, the .htaccess file seems to be a bit heterogeneous. I have taken some care about readability, but there are still some problems (such as pages with www. prefix not working). How would you improve this .htaccess?

RewriteEngine on
ErrorDocument 404 /404/

# RENAME MYWEB
RewriteCond %{HTTP_HOST} ^(www\.)?myweb\.com$
RewriteRule ^/?projects/web - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?myweb\.com$
RewriteRule ^(.*?)/?$ /projects/web/$1 [NC,L]

# RENAME WEB1
RewriteCond %{HTTP_HOST} ^(www.)?web1.myweb.com$
RewriteRule ^/?projects/web1 - [L]
RewriteCond %{HTTP_HOST} ^(www.)?web1.myweb.com$
RewriteRule ^(.*?)/?$ /projects/web1/$1 [NC,L]

# RENAME WEB2
RewriteCond %{HTTP_HOST} ^(www\.)?web2\.myweb\.com$ [NC]
RewriteRule ^(.*)$ /projects/web2/$1 [NC,QSA,L]

# RENAME WEB3
RewriteCond %{HTTP_HOST} ^(www\.)?web3\.myweb\.com$
RewriteRule ^/?projects/web3 - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?web3\.myweb\.com$
RewriteRule ^(.*?)/?$ /projects/web3/$1 [NC,L]

# RENAME WEB4
RewriteCond %{HTTP_HOST} ^(www\.)?web4\.myweb\.com$ [NC]
RewriteRule ^(.*)$ /projects/web4/$1 [NC,QSA,L]

# RENAME WEB5
RewriteCond %{HTTP_HOST} ^(www\.)?web5\.myweb\.com$
RewriteRule ^/?projects/web5 - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?web5\.myweb\.com$
RewriteRule ^(.*?)/?$ /projects/web5/$1 [NC,L]

# ALLOW HTTPS ONLY AT TELEGRAM BOTS
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !projects/bot1/index.php$ [NC]
RewriteCond %{REQUEST_URI} !projects/bot2/index.php$ [NC]
RewriteCond %{REQUEST_URI} !projects/bot3/index.php$ [NC]
RewriteCond %{REQUEST_URI} !projects/bot4/index.php$ [NC]
RewriteCond %{REQUEST_URI} !logan/bot5/index.php$ [NC]
RewriteRule ^/?(.*)$ http://%{SERVER_NAME}/$1 [R,L]

# SHORTCUTS
RewriteRule ^favicon.ico$ res/favicon.ico [NC]
RewriteRule ^robots.txt$ res/robots.txt [NC]
RewriteRule ^enter/(.*)$ res/enter/$1 [NC]
RewriteRule ^logout/(.*)$ res/logout/$1 [NC]

# DEFAULT: CUSTOM DIRECTORY LISTING
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/404/?$
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)/$ /index.php?path=$1

I can provide more detail about the format and behaviour of the pages if needed.

Usually, I test my .htaccess using this htaccess tester and manually checking it out.

\$\endgroup\$
1
  • \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. \$\endgroup\$
    – Jamal
    Commented Feb 23, 2016 at 6:27

1 Answer 1

2
\$\begingroup\$

First off, since the subdomains are closely related, use a better regex pattern:

# RENAME WEB 1 to n
RewriteCond %{HTTP_HOST} ^(?:www.)?web$1\.myweb\.com$
RewriteRule ^projects/web([1-4]) - [L]
RewriteCond %{HTTP_HOST} ^(?:www.)?web([1-4])\.myweb\.com$
RewriteRule ^(.*?)/?$ /projects/web%1/$1 [NC,L]

Similarly, the conditions for telegram bots:

# ALLOW HTTPS ONLY AT TELEGRAM BOTS
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !projects/bot[1-4]/index.php$ [NC]
RewriteCond %{REQUEST_URI} !logan/bot5/index.php$ [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R,L]

Next, for the custom shortcuts:

# SHORTCUTS
RewriteRule ^(favicon\.ico|robots\.txt|(?:enter|logout)/.*)$ res/$1 [NC]

Moreover, if these rules are indeed inside your .htaccess files, and not in the VHost configuration, or the server conf file, then you can remove the ^/? from all patterns and simply use ^ anchor.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.