Forum Moderators: phranque

Message Too Old, No Replies

htaccess not redirecting for a virtual directory

seo friendly urls with htaccess fail to redirect virtual directory

         

thetitan

3:23 am on Jul 19, 2010 (gmt 0)

10+ Year Member



I am working on optimizing one of my site's URLs to be SEO friendly.

this is the structure:

example.com/index.php?page=blah

this is my htaccess file:

RewriteEngine on
RewriteBase /
RewriteRule ^page/(.*)$ index.php?page=$1 [L]
RewriteRule ^page/ http://example.com/ [L]
RewriteRule ^page http://example.com/ [L]


I got it to work where when I enter example.com/page/blah, the page loads whatever I want it to load as index.php?page=blah. I also got it to redirect me to example.com when I enter example.com/blah. But when I enter example.com/blah/ it does not redirect back to example.com, it just loads the defaults for example.com/index.php.

I have tried using ^page/$ to no effect.

BTW, page does not exist as a directory.

Thank you for your help.

jdMorgan

3:40 am on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Problems:
  • Rules in incorrect order -- Redirects must precede internal rewrites to avoid exposing filepaths as URLs.
  • Second rule is entirely redundant with third rule, since neither rules' patterns are end-anchored.
  • Redirect type is not specified. Default is 302-Found. 301 and 302 are handled differently by search engines.

    With the code as originally-configured, page/<anything or nothing> would be rewritten to /index.php?page=<anything or nothing> and neither redirect would ever be invoked unless "page" without a trailing slash was requested.

    I looked at optimizing and correcting this code, but ran into serious inconsistencies. For example, if "page<anything>" is redirected to example.com/, then your script will never be invoked, because it's only invoked if the requested URL starts with "page/" and example.com/ doesn't contain "page/" at all.

    So it's back to the basics: What, exactly, are you trying to accomplish here? A good requirements specification is needed before coding.

    Jim
  • thetitan

    5:00 am on Jul 19, 2010 (gmt 0)

    10+ Year Member



    index.php has two variables that can be invoked - page and query .

    I tried using the script without having page, but it did not work. I would prefer example.com/blah.

    Also, I suppose it would be best if I move the query variable to a different file so it works and does not get messed up by the page htaccess settings.

    I rearranged the script and it works fine now:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^page$ http://domainrecord.info/ [L]
    RewriteRule ^page/$ http://domainrecord.info/ [L]
    RewriteRule ^page/(.*)$ index.php?page=$1 [L]


    I haven't set the redirect type yet, because I first want to figure out the page issue before I tell the search engines how to treat the URL structure.

    jdMorgan

    2:22 pm on Jul 19, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You *have* set the redirect type -- to its default, which is 302-Found. Put the flag in your rule to remind yourself to change it if needed.
    Your first two rules are redundant. Make one rule handle either case by making the trailing slash optional in the pattern:

    RewriteEngine on
    RewriteBase /
    #
    RewriteRule ^page/?$ http://example.com/ [R=302,L]
    RewriteRule ^page/(.+)$ index.php?page=$1 [L]

    The second parameter could be supported by making your 'friendly' URL-path look like "/page/query" or perhaps like "/page-query" -- If you use "/" as the delimiter, it will require that you use absolute or server-relative URLs in your on-page included-object references, such as image, media, CSS, and JS file includes.

    If you use a hyphen as a delimiter, this will not apply, but then your "page" and "query" parameters cannot contain hyphens without resulting in an ambiguity when trying to parse them.

    You could also use a few other characters as the delimiter, but all have drawbacks -- They either look funny, are hard to read (e.g. on the radio, TV, or over the phone), or both. The characters that can be used in the URL-path-part are restricted by the HTTP protocol specification, so your practical options are limited to " / - _ ~ * ' ! ". Of these, the hyphen is best because it can be easily read aloud as "dash" and unlike the underscore, it does not "hide" under the link underlining and so is always visible.

    Jim

    thetitan

    1:42 pm on Jul 20, 2010 (gmt 0)

    10+ Year Member



    Jim thank you for your help with the script and all of the other information.

    Is there a way to format the URLs like example.com/blah instead of example.com/page/blah? I have only been able to get the URLs to work by adding "page/".

    I have tried RewriteRule ^(.+)$ index.php?page=$1 [L], but the site looses all layout formatting and other functionality, even though I am using absolute paths for file like the CSS style sheet.

    When I use RewriteRule ^/(.+)$ index.php?page=$1 [L], I gain back all the layout styling and functionality, but trying to load example.com/blah does not work.

    g1smd

    10:45 pm on Jul 20, 2010 (gmt 0)

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



    The rule
    RewriteRule ^(.+)$ index.php?page=$1 [L]
    rewrites a URL request for
    example.com/stylesheet.css
    to the internal filepath
    index.php?page=stylesheet.css
    and your script fails to send a valid stylesheet out.

    You need to change the rule so that it does not activate for media file requests, or for CSS, or stylesheet URLs. Your current rule will also mess up requests for robots.txt too.

    Either add a preceding RewriteCond that excludes all such files from the rule action OR change the (.+) pattern to something a LOT more specific. If the site uses extensionless URLs something like ^([^.]+)$ might work.

    However, do NOT start any code until you have drawn up a list of which types of URLs should be handled by the PHP script and which should not.

    In most cases it is easier to have that /page/ or some other identifier present in the URL so that Mod_Rewrite has an easy job spotting which URL requests are the ones that need to be actioned.