Forum Moderators: phranque

Message Too Old, No Replies

.htaccess to remove .php and redirect

         

achshar

2:27 pm on Jun 7, 2010 (gmt 0)

10+ Year Member



Hello

i have url like:
[1]http://example.com/foo.php

i want to have it like:
[2]http://example.com/foo

and also the url [1] when entered redirects to url [2]
i know this is not alot of coding but i dont know about .htaccess. and frankly speaking i dont really want to learn it too :P

If any one out there can help...

also i have a bit of confusion.. what if there also exists an directory with name foo? what would get preference by the server? and any common solution for this?

thanks :)

achshar

11:22 pm on Jun 18, 2010 (gmt 0)

10+ Year Member



i already mentioned it before, at post #4149027 i couldn't find anchor link but its almost in middle of page where i showed up the whole .htaccess file..

but anyways here is my whole htaccess again



RewriteEngine on

AddType application/x-httpd-php .xml

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^blog\/?(.*)$ "http\:\/\/blog\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^movies\/?(.*)$ "http\:\/\/movies\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^sms\/?(.*)$ "http\:\/\/sms\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^articles\/?(.*)$ "http\:\/\/articles\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^admin\/?(.*)$ "http\:\/\/admin\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^resources\/?(.*)$ "http\:\/\/resources\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^images\/?(.*)$ "http\:\/\/images\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^js\/?(.*)$ "http\:\/\/js\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^css\/?(.*)$ "http\:\/\/css\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^feeds\/?(.*)$ "http\:\/\/feeds\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^newsletter\/?(.*)$ "http\:\/\/newsletter\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]

RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

this is all..

[edited by: achshar at 11:58 pm (utc) on Jun 18, 2010]

[edited by: jdMorgan at 1:39 pm (utc) on Jun 19, 2010]
[edit reason] Pleasu use example.com only. [/edit]

g1smd

11:35 pm on Jun 18, 2010 (gmt 0)

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



Take the escaping out of all the target URLs.

Make sure that literal periods in patterns are always escaped.

You are re-introducing errors we already fixed many posts back.


Replace this code:

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$


with this simpler code:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com



Your third rule will cause a multi-step redirection chain. It needs to be moved to be the 14th rule (just before the rewrite at the 15th) to stop that happening. [done]


Every line of code like this:

RewriteRule ^blog\/?(.*)$ "http\:\/\/blog\.example\.com\/$1" [R=301,L]


needs to change to something like this:

RewriteRule ^blog(/(.*))?$ http[i][/i]://blog.example.com/$2 [R=301,L]



Actually, renumbered rules 3 to 13 can be replaced with a single rule when using (js|blog|etc) syntax to examine the requested path and a backreference to pass that value into the target URL value.

[edited by: g1smd at 12:22 am (utc) on Jun 19, 2010]

achshar

12:00 am on Jun 19, 2010 (gmt 0)

10+ Year Member



eyeekes.. sorry no offence but i made all the changes you said to the 'extra code' but my cpanel freaked out and showed that i didnt had any subdomain redirects.. which is under home > redirects in capnel..

which is quite understandable because it can only read the code it itself made..
we already discussed that earlier..

hmm all that coding is done by cpanel.. i didnt do it.. so if i change it, cpanel wont recognise code. And maybe it will screw uo with stats, info, any any other thing that may rely on it..

you really think it's a good idea coz i dont want to mess things up.. now at-least i can control the subdomains from the cpanel.. if i change the code, they wont show up there and you see i am not that good with .htaccess after all.. php is cool but htaccess codes just dont get into my head.. plus exams coming, alot of homework.. i really cnnot afford reading documentation.. but you guys have helped me alot.. now i can atleast copy-paste small chunks of code to make it work as i want to.. :)


Personally, I want to beat the person who programmed cPanel to a pulp. It produces some of the worst, malformed, inefficient, and just blatantly wrong, .htaccess code on the planet.


and as far as the code discussed in this thread is concerned i am sure that it is up to date..

Your third rule will cause a multi-step redirection chain. It needs to be moved to be the 14th rule (just before the rewrite at the 15th) to stop that happening.

done :)

jdMorgan

1:50 pm on Jun 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please do not post your domain name here. Please use example.com only. See our Forum Charter for more info. Thanks!

To support redirects to remove "www" from subdomain as well as main-domain requests, replace

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]

with

RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]

Jim

achshar

5:59 pm on Jun 19, 2010 (gmt 0)

10+ Year Member



@jim oh i am soo sorry for that.. i must have left them there when i edited the post..
and the code you gave me does not work in one condition..
it changes www.example1.example.com/foo changes to example1.example.com/example1/foo

ie the subdomain remains intact along with the physical location of subdomain (which is example.com/example1/foo in all other cases it works perfectly..

also i changed

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://example.com/$1 [R=301,L]

TO

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index$ http://example.com/$1 [R=301,L]

because example.com/index was showing same as example.com
because one code removed .php extention and other removed index.php but /index would go in vein.. i simply changed index.php to index so that one code removes .php and other removes /index (if present) also the directory named index (i.e example.com/index/foo) also works so i guess we are all done except for one problem mentioned above.. :D

achshar

6:43 pm on Jun 19, 2010 (gmt 0)

10+ Year Member



oh shoot! i today noticed that i cannot sign in to my wordpress.. and i think i know why..!
plz plz help.. is there any way we can exclude one directory or subdomain from all the url rewrites?
my domain is blog.example.com and its physical location is example.com/blog

i have a pending update and i shall not be doing it until i could get myself in..

g1smd

6:58 pm on Jun 19, 2010 (gmt 0)

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



Using a preceding RewriteCond with the ! "NOT" operator can be used with any pattern matching.

achshar

7:15 pm on Jun 19, 2010 (gmt 0)

10+ Year Member



or may be something like
RewriteRule ^(blog) - [L]
i took it from the forum itself and it works! hurray..

thanks..
now just the thing in my above post ^^
i can see the thread has been very long but i guess that would be the last bit.. :)

jdMorgan

2:54 pm on Jun 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "example1.example.com/example1/foo" problem you report above has to do with rule order. You have placed the "remove www" redirect *after* the internal rewrite that is used to map subdomains to subdirectories.

If you are thinking that it's OK to just add new rules on at the end, then that is incorrect. Rules must be placed in a specific order and for a specific reason.

Of course, if you use Control Panel to do these internal rewrites, then you cannot use *any* external redirects in .htaccess -- All external redirect code will have to be implemented using the Control Panel.
(Frankly, if you want to have 'complex' redirects and rewrites on your site, it is time to stop using control panel. Move all of its 'code' to .htaccess, clean up that code, make a backup, and then don't use Control Panel again. Control Panel is 'easy' but it is a "beginner's" tool. And as g1smd points out, it generates really awful inefficient code.)

The oft-posted general rule of thumb is this:

  • Place access control rules (e.g. anti-hotlinking rules) first.

  • Place all external redirect rules next, in order from most-specific patterns and conditions (one or only a few requested URLs affected) to least-specific patterns and conditions (many or even all requested URLs affected). (For example, domain canonicalization redirect rules are almost always the last redirect rules in a properly-coded file.)

  • Follow the external redirects with all internal rewrites, again in order from most- to least- specific.

  • Do not mix mod_alias "Redirect" and "RedirectMatch" directives with redirects implemented using mod_rewrite "RewriteRule". If you mix modules, you cannot control execution order, as it is the server that will decide whether to process all mod_alias or mod_rewrite directives first. If you use mod_rewrite for *any* redirects or rewrites, then use it for *all* redirects and rewrites.

    In this way, you avoid multiple/stacked/chained redirects (moire than one redirect resulting from a single client request), and you avoid 'exposing' internally-rewritten filepaths as URLs to the client.

    ---

    While the "RewriteRule ^(blog) - [L]" code you added may work, be aware that it will do exactly what it says: Any requests for "/blog<anything or nothing here>" will skip all of the remaining rules in the file. This may be OK for now, but what if you later decide to add access controls -- say to prevent bad bots from ripping your blog? With that rule in place, your access control code may be skipped as well.

    The better solution was given by g1smd above -- Use a negative-match RewriteCond on specific rules to prevent them form being applied to /blog requests:

    RewriteCond %{REQUEST_URI} !^/blog/

    ---

    Instead of replacing the "/index.php" redirect with your "/index" redirect, I suggest that you do both. Either use separate rules, or make the ".php" part optional in both patterns.

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]

    ---

    Be careful about modifying rules discussed here. The recommendations you've gotten are well-thought-out and drawn from years of experience. Any changes have the risk of causing unforeseen side-effects -- things that you have not tested but that your correspondents are very-well aware of.

    RewriteRules are not 'good enough' when they appear to redirect or rewrite to the proper location. They are only good when the do that *and* they do not affect other URLs that you don't want redirected or rewritten, *and* when they do not negatively affect server performance, *and* when they do not negatively effect your search rankings and site usability. In other words, this subject is *far* deeper than just getting the code syntax correct...

    Jim
  • achshar

    7:27 pm on Jun 20, 2010 (gmt 0)

    10+ Year Member



    alright i have worked over it again from the crack and this is what i have come up with.. please have a look coz i am still unsure about the order of codes..


    RewriteEngine on

    AddType application/x-httpd-php .xml

    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]

    RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
    RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]

    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
    RewriteRule ^blog(/(.*))?$ http://blog.example.com/$2 [R=301,L]

    #similar code for other subdoamins goes here..

    RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]


    if you want to have 'complex' redirects and rewrites on your site, it is time to stop using control panel

    i believe i have to agree with this. So i changed the subdomain code to what g1smd suggested a few posts above.. and yes they work perfectly :)

    The better solution was given by g1smd above -- Use a negative-match RewriteCond on specific rules to prevent them form being applied to /blog requests

    i tried doing that as you can see in the code above but it does not work.. also it does not even throw any error

    Instead of replacing the "/index.php" redirect with your "/index" redirect, I suggest that you do both. Either use separate rules, or make the ".php" part optional in both patterns.

    i did it this way.. hope this is correct..


    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]


    Follow the external redirects with all internal rewrites

    i believe i have only one internal rewrite in my htaccess and it has been moved to end

    You have placed the "remove www" redirect *after* the internal rewrite that is used to map subdomains to subdirectories.

    internal rewrites? arent these codes redirects because whenever i change them and something goes wrong.. my subdomain redirects stop working but my subdomais are working i.e i could access my subdomain with foo.example.com but example.com/foo stops redirecting.
    also my cpanel now does not show any 'redirects' but my subdomains are still there..
    i am still unable to find where are those rewrite codes for my subdomains..

    "example1.example.com/example1/foo" problem is still there may be because of the order.. but i have tried all of the variations in order i could think of..

    EDIT:
    domain canonicalization redirect rules are almost always the last redirect rules in a properly-coded file

    non-www code is a domain canonicalization redirect.. right?

    BTW what is the value of 'HTTP_HOST' if url is [example1.example.com...]
    i tried searching a lot but couldn't find a straight forward answer..

    jdMorgan

    11:37 pm on Jun 20, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    "Most-specific to least-specific"

    AddType application/x-httpd-php .xml
    #
    RewriteEngine on
    #
    # Redirect direct client requests for /<any-directory>/index.php
    # or /<any-directory>/index to /<any-directory>/
    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.php)?$ http://%{HTTP_HOST}/$1 [R=301,L]
    #
    # Redirect direct client requests for /<any-directory>/<any-page>.php
    # to /<any-directory>/<any-page> to 'force' extensionless URLs
    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://%{HTTP_HOST}/$1 [R=301,L]
    #
    # Redirect requests for all subdomain-subdirectories to subdomains
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
    RewriteRule ^([b]blog|movies|sms|admin|resources|images|js|css|feeds|newsletter[/b])(/(.*))?$ http://$1.example.com/$3 [R=301,L]
    #
    # Remove "www" and redirect to canonical hostnames
    RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
    RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]
    #
    #
    # Rewrite all other non-media requests to script if script exists
    RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]

    The problem of seeing the subdomain-subdirectory paths in your redirected URLs is likely caused by rewrites created by control panel in the server config file. Really, there's no good way to make it work with redirects and rewrites done by both control panel and .htaccess. The work-around is to generate several more rules in .htaccess to 'catch' the situation where the control-panel-generated code has done a rewrite and has prepended the subdomain-subdirectory to the URL-path, and remove it when a redirect needs to be done. But frankly, that's too much work to get around a silly control panel...

    Jim

    achshar

    12:08 am on Jun 21, 2010 (gmt 0)

    10+ Year Member



    first of all thank you for that code :)

    but there's just one thing

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://%{HTTP_HOST}/$1 [R=301,L]

    AND

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*)index(\.php)?$ http://%{HTTP_HOST}/$1 [R=301,L]


    the above two codes gave a problem, when we go to example1.example.com/foo.php [or index.php]
    they presented the same error.. "example1.example.com/example1/foo"

    BUT when i replaced '%{HTTP_HOST}' with 'example.com' everything worked fine..
    so will it be cool if i keep it this way..? also this tells us what could be wrong in the cpanel's config file..

    it must also be containing http_host which when replaced should bring everything back to track.. the subdomain would be removed and raw directory path will be left (example.com/example1/foo) which will then be corrected by subdomain redirection code (and changed to example1.example.com/foo).. is my logic correct.. ?

    EDIT:
    oh yes the blog's url still get treated for .php and index.php.. :O
    my be it is because blog is subdomain and
    RewriteCond %{REQUEST_URI} !^/blog/
    would maybe look for something like example.com/blog and not blog.example.com

    jdMorgan

    2:53 am on Jun 21, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Your problem cannot be solved completely because the Cpanel-generated code is interfereing with your custom .htaccess code, and there's nothing that can be done about that until you get away from using cpanel for anything but very basic domain and subdomain declarations.

    I stopped using it years ago, and many of my servers don't even have a cpanel, so unfortunately, I can't tell you how to 'turn it all off' and take control into your own code.

    The rule order I described above is critical. And it has to be applied across all files containing mod_rewrite code -- the server config files as well as your .htaccess file(s). If the server config file does an internal rewrite before a external redirect is invoked (in the server config file or in your .htaccess file), then that will cause a problem -- exposing the internally-rewritten filepath as a URL to the client. That's really bad for your search ranking... :(

    ---

    I used %{HTTP_HOST} in those two new rules in an attempt to support your subdomains. If you redirect to example.com in all cases, then any request for /index or /index.php will get redirected to example.com, even if the request was for resources.example.com/index.php -- and I doubt you'd want to do that.

    So I tried to say "keep the same requested hostname, and just fix the /index.php part." And obviously, that still leaves you with un-fixed cases.

    You have to look at each rule and ask, What if the domain is 'x' and the URL-path is 'y' -- What happens then? If you are satisfied with the answer in each case, then the rule is good. If not, then the rule needs more work.

    Jim

    achshar

    3:10 am on Jun 21, 2010 (gmt 0)

    10+ Year Member



    ok i completely get your idea.. and for now i think i can end up as you gave the code.. because it all works while navigating through site.. the problem for now is only to standardize things.. and i think that can be postponed at-least until i can afford any professional doing it for me..

    plus who now days inserts www in fron of subdomains.. even browsers dont.. (obviously except ctrl+enter) so unless and untill someone manually changes the url.. my site should be good :) i am quite sure that none of the link in my site includes www or the raw path.. so lets just wait (or hope) for my site to grow.. :)

    plus i am greatly thankful to the people here for helping me out.. :)
    i got to learn alot of new stuff, thanks a ton! for this loooong thread :D

    g1smd

    7:48 am on Jun 21, 2010 (gmt 0)

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



    so unless and until someone manually changes the URL

    Oh, they can and they will, and it's too late to be trying to patch up your search rankings after it has happened and Google has indexed those duplicates.

    Google might well have already seen links pointing to www URLs in the example code you posted in this thread.

    Make sure you register both the www and non-www URLs for every subdomain of your site in Google WebmasterTools. Look very carefully at all of the reports.

    jdMorgan

    1:37 pm on Jun 21, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    What I meant by my comments above was that unless you can 'take control' away from control panel so that you can control the presence and ordering of all rules, the only way to 'fix' the 'subdomain-subdirectory-filepath exposure' problem is to duplicate each of your external redirects, and provide a mechanism to strip out that subdomain-subdirectory filepath info if it has been inserted by a cpanel rewrite at the server config level:

    You'd have to duplicate each existing redirect rule, and precede the existing one with a more-complex one:

    # Redirect rewritten client requests for /<subdomain-subdirectory>/<any-directory>/<any-page>.php to
    # <subdomain>.example.com/<any-directory>/<any-page> to 'force' extensionless URLs, excluding "/blog/"
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(movies|sms|admin|resources|images|js|css|feeds|newsletter)/(([^/]+/)*[^.]+)\.php http://$1.example.com/$2 [R=301,L]
    #
    # Redirect direct client requests for /<any-directory>/<any-page>.php to
    # /<any-directory>/<any-page> to 'force' extensionless URLs, excluding "/blog/"
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteCond %{HTTP_HOST} ^(((movies|sms|admin|resources|images|js|css|feeds|newsletter)\.)?example\.com)
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://%1/$1 [R=301,L]

    The code gets complex because you essentially have to have a bunch of rules, each of which recognizes and 'fixes' one of all possible problem combinations all at one go.

    I also want to re-emphasize that doing it this way instead of improving the hosting set-up is not a good long-term solution.

    Jim

    achshar

    4:30 pm on Jun 21, 2010 (gmt 0)

    10+ Year Member



    Make sure you register both the www and non-www URLs for every subdomain of your site in Google WebmasterTools. Look very carefully at all of the reports.

    yes, i already have all the subdomains registered along with www and non www.. about 25 of them..

    I also want to re-emphasize that doing it this way instead of improving the hosting set-up is not a good long-term solution.

    agreed.. i tried the code you gave above.. but unfortunately it did not help.. i tried both way.. preceded it with the present code.. and replaced it..

    so an idea struck me.. we could use php over here.. coz right now.. the problem comes when www is there in url.. rest every case is clean..

    and the structure of my site is as follows..
    one section (e.g movies) has only a couple of pages that are actually publicly used ..

    like index for movies takes all other info from database or through ajax.. in this way all the pages in a specific subdomain are ultimately squeezed to only 1 or at-most 2 pages (usually other one the search page) hence we can use php to remove www and give a 301. and for other pages.. they give incomplete content (e.g search suggestion ul li) so i suppose google wont crawl it.. (because i haven seen any of them on serps and webmaster tools)

    So on those two pages on every content subdomain.. (resources, sms, blog, articles and movies) we can have php redirects and for the other subdomains (css, js, ajax, images) could go with www coz google wont mind duplicate scrips or style-sheets.. and i have *only* design images.. nothing that G or any other search engine would be interested in.. how would that go? this kind of does the job.. SEO wise..

    achshar

    9:57 pm on Jun 21, 2010 (gmt 0)

    10+ Year Member



    [sorry.. 'Allowable time to edit post has past.']

    i use a header and footer file on every page of my site.. a single line of code could do it..

    how about something like..

    $array = explode('.', $_SERVER['HTTP_HOST']);
    if($array[0]=='www') {header('HTTP/1.1 301 Moved Permanently'); header('Location: http://'.$_SERVER['SERVER_NAME'].strip_tags($_SERVER['REQUEST_URI']));}

    is it safe enough.. i have strip_tags for SXX...
    This 48 message thread spans 2 pages: 48