SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Guru laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing url path with .htaccess

    Hey all,

    I am wanting to go from

    url.com/porfolio.html

    to

    url.com/portfolio/

    Can anyone direct me where I can find out how its done. I did a g search but couldnt come up with nothing but how to change long urls to shorter ones.
    Thanks In Advance

    David

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Create a .htaccess file and add the following:

    Code:
    RewriteEngine On
    RewriteRule ^porfolio.html$ http://url.com/porfolio/ [R=301,L]
    Then upload the .htaccess to the root of your domain and try navigating to url.com/porfolio.html.
    Does that work?

  3. #3
    SitePoint Guru laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    404 page
    Thanks In Advance

    David

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    M'kay

    The folder url.com/portfolio/ does exist and have an index.html file in it, doesn't it?
    Or are you just wanting to rewrite the url, but still have it point to the original file?

  5. #5
    SitePoint Guru laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I want it to rewrite the url, I knew I could put up another directory and place an index.html that has the portfolio code but I was trying to make it easier and keep the same structure.
    Thanks In Advance

    David

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Try this:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^portfolio/$ portfolio.html [NC,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio\.html [NC]
    RewriteRule ^portfolio.html$ /portfolio/ [L,R=301]
    Any joy?

    Edit: this should work for the following url scheme: url.com/porfolio.html, i.e.with the file in the top-level-directory

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

    First, do NOT change a file URI to a folder (using a trailing /). After that, have a look at the tutorial linked in my signature then come back with questions and your attempted code.

    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

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there Dk,

    I hoped you'd show up
    I hope the OP doesn't mind me carrying on this thread (if so, just say and I'll butt out), but after investing a couple of hours reading articles about mod_rewrite, I'm now curious about the best way to do this.

    Quote Originally Posted by dklynn View Post
    First, do NOT change a file URI to a folder (using a trailing /).
    Are you saying that it is a bad idea to change "url.com/portfolio.html" into "url.com/portfolio/"
    If so, why?

    Quote Originally Posted by dklynn View Post
    After that, have a look at the tutorial linked in my signature then come back with questions and your attempted code.
    I've just finished reading your tutorial (again). The first part was fairly straight forward. The rest made my head explode.

    One important thing I took away from what you wrote however, is to be explicit in what you tell Apache to do (and not to do).

    So, I guess these are the requirements.
    • If a user requests url.com/portfolio.html, then rewrite the address in the address bar to url.com/portfolio/ (not sure about slash) and then serve the file
    • If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html
    And these are the steps I would take to solve this problem:
    • Test if the entire query string exactly matches portfolio.html
    • If so rewrite the query string to portfolio/
    • Check if the query string exactly matches portfolio/
    • If so, redirect internally to portfolio.html
    Can you tell me if that is a sensible approach and maybe the sections of your tutorial I should concentrate on to accomplish this?

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi Pullo!

    P - I hoped you'd show up

    That's quite a compliment, thank you. From your prior responses, I know that you're working hard to learn mod_rewrite and have the basics down okay. The rest is all logic ... as you get to below.

    P - I hope the OP doesn't mind me carrying on this thread (if so, just say and I'll butt out), but after investing a couple of hours reading articles about mod_rewrite, I'm now curious about the best way to do this.

    It's fair to ask questions along the same line as the OP.

    P - Are you saying that it is a bad idea to change "url.com/portfolio.html" into "url.com/portfolio/"
    If so, why?

    There are several reasons:

    1. Doing so changes the folder level for internal links.

    2. Doing so makes Apache (and visitors) believe that they are linking to a folder's DirectoryIndex.

    3. Doing so has no benefit.

    4. It just makes no sense (to me) at all!

    P - I've just finished reading your tutorial (again). The first part was fairly straight forward. The rest made my head explode.

    Bummer! That must have hurt!

    P - One important thing I took away from what you wrote however, is to be explicit in what you tell Apache to do (and not to do).

    Amen.

    P - So, I guess these are the requirements.
    • If a user requests url.com/portfolio.html, then rewrite the address in the address bar to url.com/portfolio/ (not sure about slash) and then serve the file

      The OP wants to link to portfolio/ and serve portfolio.html.

    • If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html

      That's what I believe his/her intent was.
    P - And these are the steps I would take to solve this problem:
    • Test if the entire query string exactly matches portfolio.html
    • If so rewrite the query string to portfolio/
    • Check if the query string exactly matches portfolio/
    • If so, redirect internally to portfolio.html
    P - Can you tell me if that is a sensible approach and maybe the sections of your tutorial I should concentrate on to accomplish this?

    The "Redirect TO New Format" section has the code to handle a "loopy redirection."

    Pullo, IMHO, you're going about this all wrong. You're trying to redirect TO the folder then back to the file (loopy - it can be done safely but that's more advanced [and IS in the tutorial how to do that safely]).

    My list (advanced):

    • Order is critical!
    • Strip file extension (.html) by
      - Check if IS_SUBREQ to prevent looping
      - Redirect to extensionless filename (strip file extension, i.e., .html, NOT .jpg, .css, .js, etc) with R=301
    • Serve file with .html file extension by
      - Check if extensionless filename with .html extension is a file
      - Redirect extensionless filename to include .html file extension and serve (without R=301)


    I intentionally used pseudocode to leave the coding as an exercise for you but will be willing to help if you run into a roadblock (even with the "non-loopy code" in the tutorial).

    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

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi DK,

    Many thanks for the detailed response!
    I've taken a while to think this through and in light of your comments, I've rewritten the requirements:
    • If a user requests url.com/portfolio.html, then redirect to extensionless file name with R=301. This will give us url.com/portfolio and not url.com/portfolio/, but this is acceptable, as it doesn't mess with user expectation or change the folder level for internal links.
    • If a user requests url.com/portfolio or url.com/porfolio/ redirect internally to url.com/portfolio.html
    This is the code I have used to do this:
    Code:
    RewriteEngine on
    RewriteCond %{IS_SUBREQ} false
    RewriteRule portfolio.html$  /portfolio [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^portfolio/?$ portfolio.html [L]
    Unfortunately, this causes an infinite loop.
    I've done a lot of Googling and found several allusions to this being caused by the initial redirect being treated as a new request (and thus never as a sub-request), but try as I might, I can't get it to work.

    Could you shed some light on this and tell me where I'm going wrong?

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

    You did very well with your code EXCEPT for understanding that the optional trailing / is poison (two different directory levels in play) for relative links in the file.

    My "pseudo code" was for a generic extensionless file name but, if you're only using portfolio, that greatly simplifies the problem (meaning that you don't need to check if the file exists - you KNOW it does).



    [QUOTE=Pullo;5260171]Hi DK,
    Code:
    RewriteEngine on
    RewriteCond %{IS_SUBREQ} false
    RewriteRule ^portfolio\.html$  /portfolio [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME}\.html -f # not required if you know it exists
    RewriteRule ^portfolio/?$ portfolio.html [L] # POISON
    That should not result in an infinite loop BECAUSE the {IS_SUBREQ} will not be false after executing the redirection to the html file. I don't know who's saying that an internal redirection is being treated as a new request but that's incorrect (mod_rewrite DOES continue to make passes through its code until no matches/redirections are found but the {IS_SUBREQ} is set to true after the first pass (if portfolio.html was originally requested)).

    Of course, there may be other mod_rewrite code in action in your .htaccess but you've not displayed it so I can't comment on any conflicting interaction.

    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

  12. #12
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi DK,

    Thanks for your reply. I am learning a lot here!

    I tried changing the .haccess file to this:

    Code:
    RewriteEngine on
    RewriteCond %{IS_SUBREQ} false
    RewriteRule ^portfolio\.html$  /test [R=301,L]
    
    RewriteRule ^portfolio$ portfolio.html [L]
    But unfortunately still get an infinite redirect loop and the error message:

    Code:
    (Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
    There is no other code in my .htaccess file.
    The file "portfolio.html" exists and is in the top level directory of my website along with the .htaccess file.

    Am I missing something?
    Could it be a server configuration issue?

    N.B. If I change IS_SUBREQ to true, then mysite.com/portfolio serves up the file as expected. However, then navigating to mysite.com/portfolio.html also serves up the file without rewriting the url.

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

    Quote Originally Posted by Pullo View Post
    Hi DK,

    Thanks for your reply. I am learning a lot here!

    I tried changing the .haccess file to this:

    Code:
    RewriteEngine on
    RewriteCond %{IS_SUBREQ} false
    RewriteRule ^portfolio\.html$  /test [R=301,L]
    
    Redirecting to TEST breaks the loopy code - use portfolio for this test, please.
    RewriteRule ^portfolio$ portfolio.html [L]
    But unfortunately still get an infinite redirect loop and the error message:

    Code:
    (Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
    There is no other code in my .htaccess file.
    The file "portfolio.html" exists and is in the top level directory of my website along with the .htaccess file.

    Am I missing something?
    Could it be a server configuration issue?
    The change to test is weird - should not loop at all!
    N.B. If I change IS_SUBREQ to true, then mysite.com/portfolio serves up the file as expected. However, then navigating to mysite.com/portfolio.html also serves up the file without rewriting the url.
    If {IS_SUBREQ} is initially true, then you should never redirect (and display) portfolio as intended.
    Please try again after replacing /test with portfolio (no slash).

    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

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi DK,

    Sorry about the delayed response - busy, busy, busy ...

    Also, sorry for confusing things by leaving "test" in the .htaccess file.
    This was just a typo on my part and I have been testing with "portfolio" all along.

    Right, now I have this code in my .htaccess file.

    Code:
    RewriteEngine on
    RewriteCond %{IS_SUBREQ} false
    RewriteRule ^portfolio\.html$  portfolio [R=301,L]
    RewriteRule ^portfolio$ portfolio.html [L]
    Unfortunately, still no joy.
    When I navigate to either http://url.com/porfolio or http://url.com/porfolio.html
    I get redirected to: http://url.com/home/http/power/rid/54/59/53525459/htdocs/portfolio

    This was why I had the slash there in the first place.

    Hope you can shed some light on it.

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

    Indeed I can - your server is broken (needs restarted) as there is nothing in your .htaccess to redirect wildly like you've indicated.

    I would also use an ErrorDocument statement to specify that 404s should be redirected to ... your 404 script?

    Finally, it may be a "logic thing" that, if there hasn't been a subrequest, the {IS_SUBREQ} may actually be null rather than false. Use !true rather than false and see whether that helps.

    As for your trailing /, I remain adamant that trailing /'s on file names is extremely bad technique and should mess-up your server (especially since you've probably enabled MultiViews which will defeat the specific mod_rewrite you're working on).

    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

  16. #16
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,056
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there DK,

    Thanks for your reply.
    If you think the server is broken, then I think I'll leave it here.
    What we are trying to accomplish would not actually be something I would do in reality and was intended as more of an academic exercise.
    Plus the fact, the server belongs to one of the biggest hosting companies in Germany, so I don't think they'd restart it on my say so

    BTW I tried changing "false" to "!true", but that didn't help either.

    Thanks again!

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

    Well, if the host isn't aware of a "minor problem," they can't do much about it, can they.

    Bummer. Thanks for the feedback on the T/F question.

    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


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
  •