Forum Moderators: phranque

Message Too Old, No Replies

Subdomain problem with .htaccess

Problem using .htaccess in subdomain

         

gervin

3:40 pm on Dec 28, 2007 (gmt 0)

10+ Year Member



I was using the following .htaccess code to convert PHP calls to .html:
===========================================================
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^home shop.php
RewriteRule ^reviews-([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9]+)-([0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2&i=$3&p=$4&a=reviews
RewriteRule ^buy-([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9]+)-([A-Za-z]+)-([0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2&i=$3&m=$4&p=$5&a=buy
RewriteRule ^([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9+_"]+)-([A-Za-z0-9+_]+)-([a-z]+)-([0-9]+).html shop.php?c=$1&n=$2&k=$3&t=$4&s=$5&p=$6
RewriteRule ^([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9+_"]+)-([a-z]+)-([0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2&k=$3&s=$4&p=$5
RewriteRule ^([A-Za-z0-9]+)-([a-z0-9]+)-([a-z]+)-([0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2&s=$3&p=$4
RewriteRule ^([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2&i=$3
RewriteRule ^([A-Za-z0-9]+)-([a-z0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1&n=$2
RewriteRule ^page-([A-Za-z0-9+"]+).html shop.php?a=$1
RewriteRule ^([A-Za-z0-9]+)-([A-Za-z0-9_]+).html shop.php?c=$1
RewriteRule ^item-([A-Za-z0-9]+) shop.php?i=$1
RewriteRule ^search-([A-Za-z0-9+_"]+) shop.php?k=$1
=========================================================
This works great in the domain, but it can not find the .html files when placed in a subdomain. Any idea on the extra code needed?

jdMorgan

5:01 pm on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest a re-write to greatly improve efficiency as well as addressing your problem.

Assuming that this code is located in "/subdom-subdir/.htaccess" where subdom-subdir is the path to the directory in which the resources for your subdomain are stored, try:


Options +Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^home /subdom-subdir/shop.php
RewriteRule ^reviews-([a-z0-9]+)-([a-z0-9]+)-([a-z0-9]+)-([0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&i=$3&p=$4&a=reviews [NC,L]
RewriteRule ^buy-([a-z0-9]+)-([a-z0-9]+)-([a-z0-9]+)-([A-Za-z]+)-([0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&i=$3&m=$4&p=$5&a=buy [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9]+)-([a-z0-9+_"]+)-([a-z0-9+_]+)-([a-z]+)-([0-9]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&k=$3&t=$4&s=$5&p=$6 [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9]+)-([a-z0-9+_"]+)-([a-z]+)-([0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&k=$3&s=$4&p=$5 [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9]+)-([a-z]+)-([0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&s=$3&p=$4 [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9]+)-([a-z0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2&i=$3 [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1&n=$2 [NC,L]
RewriteRule ^page-([a-z0-9+"]+)\.html$ /subdom-subdir/shop.php?a=$1 [NC,L]
RewriteRule ^([a-z0-9]+)-([a-z0-9_]+)\.html$ /subdom-subdir/shop.php?c=$1 [NC,L]
RewriteRule ^item-([a-z0-9]+) /subdom-subdir/shop.php?i=$1 [NC,L]
RewriteRule ^search-([a-z0-9+_"]+) /subdom-subdir/shop.php?k=$1 [NC,L]

Changes:
  • Combine Options directives
  • Replace "[A-Za-z0-9]" with "[a-z0-9]" using [NC] (No Case) flag for case-insensitive compare.
  • Add [L] flag to stop mod_rewrite processing for this pass if the pattern matches and the rule is invoked.
  • Escape literal periods in patterns and end-anchor the pattern -- ".html" --> "\.html$".
  • Add path to subdomain-subdirectory ("/subdom-subdir") in substitution filepath.

    mod_rewrite code "converts" nothing. Rather, it tells the server where to find the real resources ("files") to serve when certain URLs are requested by the HTTP client. The "URL conversion" was done when you changed the URLs on your pages to point to .html "pages." Rather than saying "I was using this .htaccess code to convert PHP calls to .html", it is far more accurate to say, "I was using this .htaccess code to map .html URLs to PHP script calls."

    It may also be helpful to understand that once a request "lands" in your server's filespace, there is no longer any "subdomain" -- It has already been converted from a subdomain-URL to a server-internal filepath -- in this case, shown as "subdom-subdir."

    Due to the length of the patterns, the rules in this example code may wrap to multiple lines depending on your display. However, each rewrite rule must be all on one line.

    Completely flush your browser cache before testing any changes to your .htaccess code.

    Jim

  • gervin

    8:05 pm on Dec 28, 2007 (gmt 0)

    10+ Year Member



    Thanks for the detailed and quick response. I substituted the subdom-subdir with the actually subdomain name, but I am still getting a Page not found error. The domain name is http://www.example.net and the subdomain is http://books.example.net. The domain has the original .htaccess file and the subdomain has your modified version. All of the code and subdirectories in the domain exist in the subdomain, with the exception of the subdomain directories of course. Any ideas?

    [edited by: jdMorgan at 9:32 pm (utc) on Dec. 28, 2007]
    [edit reason] example.net [/edit]

    jdMorgan

    9:34 pm on Dec 28, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    What is an example of a requested URL?
    What is the actual server filepath to the the script used to serve content for that URL?
    What is in your server error log file?

    Jim

    gervin

    12:02 am on Dec 29, 2007 (gmt 0)

    10+ Year Member



    What is an example of a requested URL?
    -----------------------------------------
    For the php calls actually being made, refer to http://www.quux-foo.com/shop.php
    You can compare the urls to the HTML's you are seeing at the [examnple.net...]
    ---------------------------------------------------------

    What is the actual server filepath to the the script used to serve content for that URL?
    ----------------------------------------------------------
    This is using shared hosting. I assume that I do not have access to that.
    ----------------------------------------------------------

    What is in your server error log file?
    ----------------------------------------------------------This is using shared hosting. I assume that I do not have access to that. I assume that it would have 404 errors.

    [edited by: jdMorgan at 12:39 am (utc) on Dec. 29, 2007]
    [edit reason] No URLs please. See TOS. [/edit]

    jdMorgan

    12:44 am on Dec 29, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    "this is shared hosting..."

    Ask your host about the paths and the error log. No error log? - Then you'll need to get a proper host.

    The URLs you are requesting generally should be made up of letter/number and number-only groups separated by hyphens and ending with ".html" according to your rules above.

    I'm afraid that without all of the information requested above, I can be of little further help. :(

    Jim