Forum Moderators: phranque

Message Too Old, No Replies

Redirecting through .htaccess

         

knkk

8:26 am on Dec 1, 2009 (gmt 0)

10+ Year Member



I want to redirect a url [abc.xyz.com...] to [xyz.com....] So the file that will be accessed will be index.php of xyz.com. That index.php should have available to it both abc and 123 (so 123 will be $_SERVER['PHP_SELF'], and abc will come out of $_SERVER['HTTP_HOST']) to enable appropriate processing.

[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!

g1smd

10:08 am on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code have you tried so far?

Make sure that each line of code is commented as to what it should do.

jdMorgan

12:33 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, what server version are you on?

If on Apache 2.x, see the Apache core AcceptPathInfo directive.

Jim

knkk

1:54 pm on Dec 1, 2009 (gmt 0)

10+ Year Member



thank you, g1smd and jim. i have unfortunately removed everything i tried as it did not work. i have been trolling around on the web trying to find a solution, but all threads seem to end ambiguously...

i am checking on AcceptPathInfo...

knkk

1:58 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



I found the solution for my subdomain subdirectory redirection problem above, here it is:

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.

g1smd

2:08 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is very very inefficient code as it makes two file or directory 'exists' checks for every object requested from your server.

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.

jdMorgan

2:24 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Move the RewriteCond for favicon.ico to the top of the RewriteCond list.

Then, if you'd like to double the speed of this code, change it to:


RewriteCond $1 !^(index\.php¦favicon\.ico)$

This avoids two unnecessary filesystem reads for favicon requests and for the case wher you rewrite the URL to /index.php and mod_rewrite restarts, as it will in a .htaccess context. There is no reason to make two extra and unnecessary calls to the operating system, potentially invoking two reads of the physical disk, as this is very slow and inefficient.

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)$

This is an apparently-minor tweak that can almost double the speed of your server and reduce wear-and-tear on your hard drive as well.

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

knkk

2:32 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



Thank you, g1smd and Jim. I will try to implement these suggestions, and post the results here shorlty.