Forum Moderators: phranque
[abc.xyz.com...] already goes to [xyz.com...] (though configuration by my service provider), and abc can be extracted out of $_SERVER['HTTP_HOST']. However, [abc.xyz.com...] seems to be looking for a folder called 123 in the root directory of xyz.com, and is showing up a 404 error when it cannot find any.
Can someone kindly give me the line(s) to be put in .htaccess to enable [abc.xyz.com...] to call index.php of [xyz.com...] (with 123 being available though $_SERVER['PHP_SELF']? I cannot seem to get it to work.
Thank you very much for your time!
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URL} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
q will contain the $_SERVER['PHP_SELF'] (123) that I mention.
The .* pattern should be tightened up so that the pattern only matches the URLs it needs to, i.e. just those for HTML pages, and not URLs for images, CSS files, robots.txt and so on.
Then, if you'd like to double the speed of this code, change it to:
RewriteCond $1 !^(index\.php¦favicon\.ico)$
Also, consider designing and adding a favicon to your site -- They are great for 'branding' and for use as bookmark and desktop shortcut icons. There are several free or trialware favicon generators that can produce multiple-resolution favicons (16x16, 32x32, 48x48, all in one file) for these purposes, and you should take advantage of them instead of throwing a 404.
It is also fine to exclude any file that you know exists and is fetched often from your rule, such as favicon.ico, robots.txt, sitemap.xml, and search engine webmaster authentication/validation files. Any file which is requested often, *should* always exist, and should never be rewritten to your script can be excluded from the rule above to avoid unnecessary disk checks.
Generalizing this advice for any Web site application, these are the two important points. To avoid unnecessary calls to the operating system to go check the disk to see if files or directories exist:
1) RewriteConds checking file- and directory- exists should always be done last
2) The RewriteRule pattern and the other RewriteConds which precede the 'exists' checks should be as specific as possible.
3) Frequently-requested URLs and URL extensions which are known to resolve to existing files should be explicitly excluded from the rule. For example, you could also manually exclude all urls ending in ".css", ".gif", and ".jpg" on most sites. For example:
RewriteCond $1 !(^index\.php¦\.(gif¦jpe?g¦ico¦css)¦^robots\.txt)$
As with all threads posted here, replace the broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.
Jim