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.
-
\$\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– Jamal2016年02月23日 06:27:02 +00:00Commented Feb 23, 2016 at 6:27
1 Answer 1
First off, since the subdomains are closely related, use a better regex pattern:
# RENAME WEB 1 to n
RewriteCond %{HTTP_HOST} ^(?:www.)?web1ドル\.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.