SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with htaccess

    Hello,

    I am building my new site. My old homepage was accessible under mydomain.com/web/index.php
    I used to use a simple meta-refresh (shame) in mydomain.com/index.php, but for my new site I want .htaccess file.
    However, I do (ab)use my site also for other webpages. So I have some photoalbums under mydomain.com/newyork and mydomain.com/montreal etc.

    My new homepage is under mydomain.com/new/index.php
    I changed basically everything so I don't need a link specific redirect.
    But I do want:
    - every request to mydomain.com to go to mydomain.com/new/index.php
    - every direct request to an old page e.g. mydomain.com/web/house/house1.php to go to mydomain.com/new/index.php
    - but every request to my other webpages like mydomain.com/newyork to be unaltered.

    1) What should my .htaccess look like?

    Would this do the trick?

    redirect 301 /index.php http://www.mydomain.com/new/index.php
    redirect 301 /web/ http://www.mydomain.com/new/index.php

    2) And if I put the .htaccess in the root (mydomain.com) do I also need to put a index.php file in the root?
    (If so, what should it contain?)

  2. #2
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wordpress htaccess or own redirect?

    Hello,

    I am building my new site based on Wordpress. My old homepage was accessible under mydomain.com/web/index.php
    I used to use a simple meta-refresh (shame) in mydomain.com/index.php, but for my new site I want to use .htaccess file.
    However, I do (ab)use my site also for other webpages. So I have some photoalbums under mydomain.com/newyork and mydomain.com/montreal etc. I use this to quickly share holiday photo's with friends and collegues and remove them after a month or so. Still building a permanent photo-site.

    My new wordpress homepage is under mydomain.com/new/index.php. I don't mind visitors seeing the /new/ sublevels. I changed basically everything so I don't need a link specific redirect from my old site.
    But I do want:
    - every request to mydomain.com to go to my wordpress site - mydomain.com/new/index.php
    - every direct request to an old page e.g. mydomain.com/web/house/house1.php to go to mydomain.com/new/index.php
    - but every direct request to my other webpages like mydomain.com/newyork to be unaltered.

    1) Should I better use Wordpress handle the redirect/rewrite with htaccess or use a simple redirect?

    2) Suppose I make my own redirect, what should my .htaccess look like?

    Would this do the trick?

    redirect 301 /index.php http://www.mydomain.com/new/index.php
    redirect 301 /web/ http://www.mydomain.com/new/index.php

    3) And if I put my own .htaccess in the root (mydomain.com) do I also need to put an index.php file in the root?
    (If so, what should it contain?)

    Help is appreciated, need to get my site live this weekend and I don't want it to be offline because I screw up htaccess files.

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

    First, WELCOME to SitePoint!

    Second, your first "want" id mutually exclusive with the third "want" so you'll never get to newyork.

    Third, you've actually broken the code for doing the impossible (from your specification)! With /index.php being redirected to /new/index.php (you don't need to have this ABSOLUTE redirection be external), you've resolved the index.php part of your spec (not the EVERYTHING). The second Redirect handles the second part of your specification and the third (no redirection) handles itself!

    Now, back to the EVERYTHING part of redirecting to new/index.php. You will need mod_rewrite for that to check whether the other requests (newyork and montreal, etc) exist before redirecting. For that, check the {REQUEST_FILENAME} NOT being -f and -d before redirecting .? to new%{REQUEST_URI}.

    If you don't understand that, please have a look at some of the coding examples in my signature's tutorial before creating your code and testing then come back with questions (in other words, I don't like spoonfeeding because you won't learn anything if I merely provide you with the code - be assured that I will be sure you get to the solution, though).

    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

  4. #4
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    Well I did some study and understand it somewhat more.

    Second, your first "want" id mutually exclusive with the third "want" so you'll never get to newyork.
    What I meant is anyone typing mydomain.com (with www, which I omit because sitepoint tunrs it into a url format) in his browser gets to mydomain.com/new/ (with www).

    Third, you've actually broken the code for doing the impossible (from your specification)! With /index.php being redirected to /new/index.php (you don't need to have this ABSOLUTE redirection be external), you've resolved the index.php part of your spec (not the EVERYTHING). The second Redirect handles the second part of your specification and the third (no redirection) handles itself!
    Eerrr, how should I interpret this? Are the exclamation marks ironical or meant as a compliment?

    Anyway, let's see what I think I should do.
    As far as I now understand, the following code seems to fulfil requirements one and three:
    - not redirect if the request is already made to mydomain.com/new,
    - AND to redirect everything to mydomain.com/new if it is a request to a non-existing file or folder.
    - R=301 means permanent redirection.

    <IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^/new/index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /new/index.php [R=301,L]
    </IfModule>

    Sometimes I find the last rule written as RewriteRule ^$ /new/ [R=301,L]
    Is it better to use . or ^$ (question)?

    Now I need to redirect requests to my old sites.
    So I need to add RewriteRule ^/web/$ /new/index.php [R=301,L]
    I just put it below the first rule.

    So the full one will read:

    <IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^/new/index.php$ - [L]
    RewriteRule ^/web/$ /new/index.php [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /new/index.php [R=301,L]
    </IfModule>

    How about this?

    Question:
    My new site is actually a Wordpress site. So under new there is htaccess as well. They use
    RewriteBase /new/
    I fail to understand what that does exactly. I read an explanation that base is used to be added as a prefix to every rewrite rule. http://randomtype.ca/blog/the-wordpr...ile-explained/ But that didn't seem logical, because the Rewrite rule is still RewriteRule . /new/index.php [L] If the Base would be added the rewrite would read new/new/index.php
    .

  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oke, I have caught up a little more and it seems I can remove the IfModule rule (read your rant about it, haha).
    And the first ReWriteRule seems superfluous, because the first RewriteCond handles this. (Wonder why Wordpress guys include it. It is supposed to prevent looping, but I don't know)

    So would this code do?
    Code:
    RewriteEngine On
     
     RewriteRule ^/web/$ /new/index.php [R=301,L]
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule . /new/index.php [R=301,L]

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

    First, to remove the automatic creation of links, look under the message textarea and uncheck the "Automatically parse links in text" box. That will take care of your www problem!

    So, if your visitor types www.example.com/, you want it sent to www.example.com/new/, correct? IMHO, UNLESS you want to use your DocumentRoot for something else, simply change the DocumentRoot of your domain (redirect within cPanel). Okay, that's not your question so I'll go on.

    The first exclamation point was a "gudonya," a compliment as you'd performed a miracle ... until we both realized that you are not redirecting everything as you'd intended. The second was a "whew" moment as you didn't need to do anything to satisfy your "do nothing" requirement.

    PLEASE wrap your code in [code]...[/code] tags as that preserves the code when quoting for a reply.

    Your specification is:

    As far as I now understand, the following code seems to fulfil requirements one and three:
    - not redirect if the request is already made to mydomain.com/new,
    - AND to redirect everything to mydomain.com/new if it is a request to a non-existing file or folder.
    - R=301 means permanent redirection.

    Then your code is:

    Code:
    <IfModule mod_rewrite.c>
    # Yes, I read your next post but couldn't let that stand :lol:
    RewriteEngine On
    
    RewriteRule ^/new/index.php$ - [L]
    # passthrough - ditto the comment above, though
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /new/index.php [R=301,L]
    # What about the domain request, i.e., no characters?
    # Simply change the . to .? (optional anything)
    # Then the leading / in the redirection is an absolute redirection
    # which is not necessary from the DocumentRoot
    </IfModule>
    Sometimes I find the last rule written as RewriteRule ^$ /new/ [R=301,L]
    Is it better to use . or ^$ (question)?

    Really? Too dumb for me! I prefer (as above) to use .? as there is no need to match anything.

    Now I need to redirect requests to my old sites.
    OMG, your old site was in the web directory?

    Question:
    My new site is actually a Wordpress site. So under new there is htaccess as well. They use
    RewriteBase /new/
    I fail to understand what that does exactly. I read an explanation that base is used to be added as a prefix to every rewrite rule. http://randomtype.ca/blog/the-wordpr...ile-explained/ But that didn't seem logical, because the Rewrite rule is still RewriteRule . /new/index.php [L] If the Base would be added the rewrite would read new/new/index.php
    .
    Well, I have to admit that I can't get my head around any valid reason to use RewriteBase. The RewriteBase directive was created to UNDO a mod_alias Redirect so that mod_rewrite could work on it. WP alters that philosophy and uses it to force the directory in which it resides. RewriteBase simply tells Apache to redirect everything to the directory specified but, if you understand mod_rewrite, you're already doing that with your code.

    Quote Originally Posted by Karaat View Post
    Oke, I have caught up a little more and it seems I can remove the IfModule rule (read your rant about it, haha).

    You do know that you saved yourself being exposed to the Standard Rant #4 with that comment, don't you? and

    And the first ReWriteRule seems superfluous, because the first RewriteCond handles this. (Wonder why Wordpress guys include it. It is supposed to prevent looping, but I don't know)

    That's something relatively new from the WP gang and demonstrates their lack of understanding of mod_rewrite. Okay, I'll cut them some slack for that as they're chasing modules all over the place to get a simple page written from snippets grabbed from "hither and yon." I can't keep track of all their modules (it's not worth my time trying to follow their routing when a simple site search can find the text or identifier I want to modify).

    So would this code do?
    Code:
    # :tup:
    RewriteEngine On
     
    RewriteRule ^/web/$ /new/index.php [R=301,L]
    # Redirect from the EMPTY web directory to new/index.php.
    # Why not eliminate the end anchor so EVERYTHING requested
    # from the web directory gets redirected instead?
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .? /new/index.php [R=301,L]
    That works for me (as modified)! IT would redirect everything requested from web/ to new/index.php then check that a the requested file doesn't exist as a file or as a directory then redirects to new/index.php. That seems to be what you're asking for so you're done (and have learned something while you were at it). Congratulations, you're a mod_rewrite guru ... well, practitioner with a good grasp of what you're doing!



    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

  7. #7
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks for the help!

    Ah, the $ is an "anchor". What's that? I read a bad tutorial that $ would tell where [this] ends and [that] starts: Rewrite [this]$ (to) [that]. As I understand it now, it will only match now if www.doamin.com/web/ is requested, which is not good. So Oke, I change it to:

    Code:
    RewriteEngine On
     
    RewriteRule ^/web /new/index.php [R=301,L]
    # Redirect from the web directory to new/index.php.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .? /new/index.php [R=301,L]
    Thanks to your help I do understand it much better. I could have cheated my way out partly, by letting workpress do the redirection to new. But I don't like them to fiddle with my root .htaccess without me understanding . If they provide an upgrade of their software this may get screwed up. Or if I want to have another site running under my root, I wouldn't know how to make them coexist. And it wouldn't have redirected the requests to the old website, which may be bookmarked by our previous visitors.So being in control feels much better. Thanks for that.

    So, if your visitor types www.example.com/, you want it sent to www.example.com/new/, correct? IMHO, UNLESS you want to use your DocumentRoot for something else, simply change the DocumentRoot of your domain (redirect within cPanel). Okay, that's not your question so I'll go on.
    Well my site is hosted at one.com. So I don't know where cPanel would reside, if I can control it at all.

    Further, my current htaccess in the root contains

    Code:
    # -FrontPage-
    
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    
    <Limit GET POST>
    order deny,allow
    deny from all
    allow from all
    </Limit>
    <Limit PUT DELETE>
    order deny,allow
    deny from all
    </Limit>
    I have never worked with Frontpage, used Dreamweaver for my old site, so I almost assume this is some kind of default from one.com. But what should I do with it? The first rule seems daunting...

    Finally, with the new htaccess in place, do I need to have a index.php or .html in my root? I guess not, but just to be sure.

    (I will make the switch to my new website later this weekend I hope. So I won't put the htaccess up until the new wordpress site is ready. Will let you know whether some unexpected behaviour occurs.)

    Regards,

    Menno

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

    Use {HTTP_REFERER} to test that the request to your action script came from your form's script - I have no problem with that at all.

    CAPTCHAs of various types can be too difficult for any automated technique. When thinking about security, please remember that security is a trade-off with both cost and complexity (for you as a coder as well as for the visitor). If you're protecting a bank vault, then it's worth a LOT of cost but, if you're protecting a database of jokes (sorry, Kevin), then it's not worth much effort at all.

    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
  •