SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How to rewrite a URL with htaccess?

    How to rewrite this URL with htaccess
    Code:
    http://domain.com/subDomain/file.ext?anything
    to
    Code:
    http://subDomain.domain.com/file.ext?anything
    and return a 301 code?

    Thanks.
    Denny Schlesinger
    web services

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Code:
    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^subDomain(.*)$ http://subDomain.domain.com$1 [L,R=301]
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks but it does not work, no redirection happens
    Code:
    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^cxxxxxxxxxxxxc\.com$ [NC]
    RewriteRule ^axxxa(.*)$ http://axxxa.cxxxxxxxxxxxxc.com$1 [L,R=301]
    Besides, the RewriteCond is not all
    Code:
    http://domain.com/anything
    but only
    Code:
    http://domain.com/subDomain/anything
    Denny Schlesinger
    web services

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    At a glance, it looks like ScallioXTX's code should work. What's the URL you're typing in the address bar? Is there anything else in your htaccess file?
    "First make it work. Then make it better."

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by captainccs View Post
    Besides, the RewriteCond is not all
    Code:
    http://domain.com/anything
    but only
    Code:
    http://domain.com/subDomain/anything
    What do you mean by that!? The RewriteCond must only contain the host, no part of the URL path whatsoever.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    What do you mean by that!? The RewriteCond must only contain the host, no part of the URL path whatsoever.
    First off, your code works! Thanks. I'm just now writing up why it's not working for me. I'll post it soon.

    I'm a GUI kind of guy, I really dislike regex, command line, unix, rewrite and stuff like that. I do it only when I must. What I meant was not the syntax of RewriteCond but my logic, I need to redirect only
    Code:
    http:mydomain/sub-domain/anything-here
    Sorry for the confusion.
    Denny Schlesinger
    web services

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    At a glance, it looks like ScallioXTX's code should work. What's the URL you're typing in the address bar? Is there anything else in your htaccess file?
    Great question! ScallioXTX's code does work, the problem is the second .htaccess file.

    I have a main domain and a sub-domain (there will be many more)
    Code:
    http://condominiospcc.com/
    http://avila.condominiospcc.com/
    This is on a shared LAMP server. In the file structure the sub-domain is a directory (folder) inside public_html
    Code:
    /home/user/public_html/avila/
    The domain has an htaccess file as does each subdomain, they are mostly identical except for the redirection I'm trying to set up.

    Apache looks at all the htaccess files. When a visitor uses the sub-domain, Apache only looks at the htaccess file in the sub-domain and everything works fine. When the visitor does not use the sub-domain Apache first looks in the public_html folder and then in the sub-domain folder. The redirecton stops working when Apache reaches this code in the sub-domain's htaccess file.
    Code:
    # other stuff up to here no problem
    
    RewriteEngine on
    
    # defeat robot exploits
    RewriteCond %{QUERY_STRING} http:// [OR]
    RewriteCond %{REQUEST_URI} http:// [OR]
    RewriteCond %{QUERY_STRING} http%3A%2F%2F
    RewriteRule ^(.*)$ - [F]
    
    # defeat robot exploits
    RewriteCond %{QUERY_STRING} DECLARE%20@S%20CHAR [OR] 
    RewriteCond %{QUERY_STRING} SET%20@S=CAST
    RewriteRule ^(.*)$ - [F]
    I wrote code to do the redirection with php but I was hoping to be able to do it with htaccess.
    Denny Schlesinger
    web services

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Which .htaccess did you put it in? I'd recommend putting it at the very top of the .htaccess in the public_html/ (i.e., not in the subdomain directory). That should work.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Which .htaccess did you put it in? I'd recommend putting it at the very top of the .htaccess in the public_html/ (i.e., not in the subdomain directory). That should work.
    That's how I had it and it does not work. I tried putting it in the sub-directory and it does not work either.

    Denny Schlesinger
    web services

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    That's how I had it and it does not work. I tried putting it in the sub-directory and it does not work either.

    If you have rewrite rules in the avila htaccess, then those will override the rewrite rules in the public_html htaccess. Your best bet is to keep all your rules in just one htaccess file -- the public_html htaccess file -- and use conditions for anything that you want to apply to only one particular domain.
    "First make it work. Then make it better."

  11. #11
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    If you have rewrite rules in the avila htaccess, then those will override the rewrite rules in the public_html htaccess. Your best bet is to keep all your rules in just one htaccess file -- the public_html htaccess file -- and use conditions for anything that you want to apply to only one particular domain.
    If things worked according to plan visitors to the main domain would use the main domain and visitors to the sub-domains would use the sub-domains. I'm creating a condo management site, the main domain belongs to the administrator and each building has it's own sub-domain. This works perfectly well. Each building can have it's own styling, etc., etc., etc.

    The issue is that a visitor can get to a building sub-domain through the main domain (domain/subDomain/) and then the server variable has the wrong "HTTP_HOST" and this throws a monkey wrench into my code. I could manage that but I want visitors to come in through the front door which is the reason for the code 301.

    If the visitor goes to a sub-domain then the public_html htaccess is not read by Apache which is the reason I need the htaccess files in public_html and in each sub-domain.

    I wrote some php code (which seems to have vanished) to do the redirect and if I can't get htaccess to do it I'll have to use it.
    Denny Schlesinger
    web services

  12. #12
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    If things worked according to plan visitors to the main domain would use the main domain and visitors to the sub-domains would use the sub-domains.
    The reason things are getting a bit tricky is because the public root of each subdomain is nested inside the public root of the main domain. So when someone accesses domain.com/avila, that's a real directory they're accessing, with its own htaccess. I see two solutions that would make your site's behavior more simple and predicable.

    1) The subdomains don't get to have htaccess files, and all rules must reside in the public_html/.htaccess file.

    Or 2) Un-nest the subdomains from the main domain. Change your filesystem directory stucture to something like this:

    main_domain/public_html/
    avila/public_html/
    other_subdomain/public_html/
    "First make it work. Then make it better."

  13. #13
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Jeff:

    I mentioned that I'm using a shared LAMP server. I don't have control over the file structure of sub-domains and if a visitor arrives via sub-domain URL then the public_html htaccess file is not read.

    I just redid the rewrite code
    PHP Code:
    if($_SERVER['HTTP_HOST'] == 'myDomain.com') {
        
    $domain 'subDomain.myDomain.com';
        
    $protocol $_SERVER['SERVER_PROTOCOL'];
        
    header("{$protocol} 301 Moved permanently");
        
    header("Location: http://{$domain}/");
        exit;

    Denny Schlesinger
    web services

  14. #14
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    ...if a visitor arrives via sub-domain URL then the public_html htaccess file is not read.
    Unfortunately the public_html htaccess will also not be read (-ish) if the user requests the subdomain directory (that is, domain.com/avila). That's why the redirect isn't working. So it looks like you're stuck with either option 1 above, or your PHP solution (or switching to a different host ).
    "First make it work. Then make it better."

  15. #15
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Unfortunately the public_html htaccess will also not be read (-ish) if the user requests the subdomain directory (that is, domain.com/avila). That's why the redirect isn't working.
    Wrong! Both htaccess files are read (public_html and avila). When I remove the rewrite stuff from the avila sub-domain and leave on the one in public_html htaccess, the redirect works. It doesn't like rewrite stuff in both htaccess files.

    ... or switching to a different host
    No way! I love my hosting service. I've been with them for just over ten years with hardly a problem. Great uptime and great help-desk which I need to use very seldom. I had two crummy hosts before this one and I'm not risking another crummy one.

    Denny Schlesinger
    web services

  16. #16
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    Wrong! Both htaccess files are read (public_html and avila). When I remove the rewrite stuff from the avila sub-domain and leave on the one in public_html htaccess, the redirect works. It doesn't like rewrite stuff in both htaccess files.
    Yes, I know. I mentioned that behavior in post #10 above. In my last post, I tried to simplify the explanation with an "-ish", but I suppose I'll be more explicit this time.

    When either domain.com/avila or avila.domain.com is accessed, then both htaccess files are read. But if you have rewrite rules in both, then the avila rewrites will override the public_html rewrites. That's why it appears as if the public_html htaccess is not read.
    "First make it work. Then make it better."

  17. #17
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Yes, I know. I mentioned that behavior in post #10 above. In my last post, I tried to simplify the explanation with an "-ish", but I suppose I'll be more explicit this time.

    When either domain.com/avila or avila.domain.com is accessed, then both htaccess files are read. But if you have rewrite rules in both, then the avila rewrites will override the public_html rewrites. That's why it appears as if the public_html htaccess is not read.
    Then surely if you put the redirect in the htaccess in the avila subdirectory it should work, no?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  18. #18
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Then surely if you put the redirect in the htaccess in the avila subdirectory it should work, no?
    ::slaps forehead::

    You're absolutely right. And it would require only minor modification from what you originally posted.

    Instead of

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


    it would be

    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://subDomain.domain.com$1 [L,R=301]
    "First make it work. Then make it better."

  19. #19
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Isn't there a rule that tells Apache to end reading/executing the htaccess commands?
    Denny Schlesinger
    web services

  20. #20
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    -ish.

    Rewrite rules have a "last" flag, which tells Apache to not apply any more rewrites. *BUT*, after a rewrite has changed the URI, then Apache will re-process that URI as if it were a brand new request, which means the rewrite phase will execute again. *UNLESS* the rewrite resulted in an external redirect, in which case it will just send the redirect response.

    *ALSO*, the "last" flag only applies to rewrite rules. Other directives such as DirectoryIndex, Options, Redirect, etc., from both htaccess files will still be processed.

    There's a lot of nuance here, and if you want to be certain you've got all the details, there's no better place than the official documentation. You can read about how htaccess directives are applied, and about how rewrite rules are processed.
    "First make it work. Then make it better."

  21. #21
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Isn't a 301 response "external?"
    Denny Schlesinger
    web services

  22. #22
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    Isn't a 301 response "external?"
    Yes.
    "First make it work. Then make it better."

  23. #23
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Capt CSS,

    Actually, NO!

    The 301 status code tells browsers (and SE's) that the redirection is permanent. The result, as far as mod_rewrite is concerned, is that you will view the redirection in the browser's location box.

    An "external" redirection is when you use the full URL in the redirection, i.e., http://subdomain.domain.com/whatever. The "external" part comes from the protocol (http://) and domain (subdomain.domain.com) used.

    On the right track, albeit painfully, in this thread. To clarify, though, mod_rewrite only stops processing when it makes no matches (allowing Apache to serve the resultant {REQUEST_URI}). JM correctly stated that the Last flag simply tells mod_rewrite to restart its processing ("take it from the top" with the new {REQUEST_URI}).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  24. #24
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Actually, NO!

    An "external" redirection is when you use the full URL in the redirection, i.e., http://subdomain.domain.com/whatever. The "external" part comes from the protocol (http://) and domain (subdomain.domain.com) used.
    An external redirect is any 3xx response. And Apache doesn't require us to use the full URL do so.
    "First make it work. Then make it better."

  25. #25
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    PLEASE, learn something before making more erroneous comments here!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •