Results 1 to 6 of 6

Thread: Trouble with htaccess

  1. #1
    Join Date
    Mar 2012
    Beans
    15

    Trouble with htaccess

    Hello,

    I have some problems with htaccess.

    Currently I have two separate files, one for localhost, one for the server.

    Localhost version is here:
    Code:
    <ifModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    
        RewriteRule ^([^/\.]+)/?$ /onnevalgus/index.php?page_type=$1 [L,NC]
        RewriteRule ^pages/([^/\.]+)?$ /onnevalgus/index.php?page_type=pages&page_id=$1 [L,NC]
    </ifModule>
    
    ErrorDocument 404 /onnevalgus/index.php?page_type=404
    And the server version:
    Code:
    <ifModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    
        RewriteRule ^([^/\.]+)/?$ /new/index.php?page_type=$1 [L,NC]
        RewriteRule ^pages/([^/\.]+)?$ /new/index.php?page_type=pages&page_id=$1 [L,NC]
    </ifModule>
    
    ErrorDocument 404 /new/index.php?page_type=404

    However, this solution is quite inconvenient, so I decided to join them together as one single file.
    I came out with following solution, but turns out it's not working properly:
    Code:
    <ifModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    
        RewriteCond %{HTTP_HOST} ^localhost
        RewriteRule ^([^/\.]+)/?$ /onnevalgus/index.php?page_type=$1 [L,NC]
        RewriteRule ^pages/([^/\.]+)?$ /onnevalgus/index.php?page_type=pages&page_id=$1 [L,NC]
        ErrorDocument 404 /onnevalgus/index.php?page_type=404
    
        RewriteCond %{HTTP_HOST} ^(www\.)mysite.com
        RewriteRule ^([^/\.]+)/?$ /new/index.php?page_type=$1 [L,NC]
        RewriteRule ^pages/([^/\.]+)?$ /new/index.php?page_type=pages&page_id=$1 [L,NC] # NOT WORKING!
        ErrorDocument 404 /new/index.php?page_type=404
    </ifModule>
    Take a look at the comment in the code: everything else seems to work, except that particular line. I just can't figure out where is the bug.

    (Phrase mysite.com is replaces the actual domain name, but everyting else is identical with the original.)

  2. #2
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,757
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Trouble with htaccess

    If the two virtual hosts are defined in separate files under /etc/apache2/sites-available/ just put the appropriate code for each site in its corresponding virtual host definition file.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  3. #3
    Join Date
    Mar 2012
    Beans
    15

    Re: Trouble with htaccess

    Actually, this server is not a virtual host in my computer, it is actual server up in the web. And I don't have any access to it's configuration files.

    Both .htaccess files for localhost and server work nice. Problem starts when I try to put them together into one single file.
    Last edited by uncleheinz; March 29th, 2013 at 06:18 PM.

  4. #4
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,757
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Trouble with htaccess

    Well, htaccess files are directory-specific. They have to reside in the virtual host's DocumentRoot. A request for one virtual server won't be seen by any others. Essentially htaccess files just allow you to replace directives that would otherwise appear in a "<Directory>" container with the same directives in a file in the directory itself.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  5. #5
    Join Date
    Mar 2012
    Beans
    15

    Re: Trouble with htaccess

    I suspect there may be something wrong with the RewriteCond statemet.

    Without RewriteCond:
    Code:
    <ifModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    
        RewriteRule ^([^/\.]+)/?$ /new/index.php?page_type=$1 [L,NC] # WORKS NICE
        RewriteRule ^pages/([^/\.]+)?$ /new/index.php?page_type=pages&page_id=$1 [L,NC] # WORKS NICE
    </ifModule>
    
    ErrorDocument 404 /new/index.php?page_type=404 # WORKS NICE

    With RewriteCond:
    Code:
    <ifModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    
        RewriteCond %{HTTP_HOST} ^localhost
        RewriteRule ^([^/\.]+)/?$ /onnevalgus/index.php?page_type=$1 [L,NC] # WORKS NICE
        RewriteRule ^pages/([^/\.]+)?$ /onnevalgus/index.php?page_type=pages&page_id=$1 [L,NC] # WORKS NICE
        ErrorDocument 404 /onnevalgus/index.php?page_type=404 # WORKS NICE
    
        RewriteCond %{HTTP_HOST} ^(www\.)mysite.com
        RewriteRule ^([^/\.]+)/?$ /new/index.php?page_type=$1 [L,NC] # WORKS NICE
        RewriteRule ^pages/([^/\.]+)?$ /new/index.php?page_type=pages&page_id=$1 [L,NC] # NOT WORKING!
        ErrorDocument 404 /new/index.php?page_type=404 # WORKS NICE
    </ifModule>
    I still don't understand why that on line there is problematic while everything else works.
    Last edited by uncleheinz; March 29th, 2013 at 07:25 PM.

  6. #6
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,757
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Trouble with htaccess

    Because, as I said, only one virtual host will be associated with that file, and it will never see the other hostname you are trying to match with RewriteCond. Just put separate files in the separate DocumentRoot directories.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

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
  •