treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] .htaccess mod_rewrite

  • How would I go about doing this:

    I want all urls that are:

    domain.org/directory/file-name.html

    to be redirected to that same file name but with an expanded directory path

    domain.org/directory/subdirectory/file-name.html

    I am imagining/learning that this is possible, but must be done via a 301 permanent redirection. I just can't seem to cut through the formatting for .htaccess files to accomplish this. My CMS is taking all my relative links and turning them into root relative... so taking: "../directory/file-name.html" and turning it into "/directory/file-name.html"

    So a link that leads to:

    domain.org/directory/subdirectory/assets/style.css

    via relative links for example, is being turned into:

    domain.org/directory/assets/style.css

    And breaking.

    Any help would be greatly appreciated.

    [reposting in General and CMS]

    Thanks,
  • RewriteEngine on
    RewriteBase /directory/
    RedirectMatch 301 (.*)\.html http://domain.org/directory/subdirectory$1


    Put this in a .htaccess file in the /directory/ folder. Make sure to change your domain accordingly.
  • I'll be trying this tonight and will get back, thanks so much for your insight John.

    If this works, it's saved my client $120/yr from having to use a subdomain (expensive host for addons - project has dragged on too long with the client to go through a host switch) which would work with this cms, and there MUST be a division from the root for two differing, but under the same domain.org umbrella... (domain.org/directory1/index.html and domain.org/directory2/index.html)

    Thanks again,
  • dratts... this doesn't seem to work. Here is what I have:


    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*)\.shtml http://trinityportland.org/our-school/about/$1
    # END DIRECTORY REDIRECTION

    Things I changed:
    [list]the /directory/
    the domain/subdirectory/
    the .html to .shtml[/list:u]
    Perhaps (apologies, but hopefully) I was just errant in how I described my goal and current situation:

    [list]http://www.trinityportland.org/our-school/index.shtml
    http://www.trinityportland.org/our-school/about/index.shtml[/list:u]
    is the structure of my site.

    The CMS (*sigh*), when encountering relative links (../about/index.shtml) is rewriting that markup as (/about/index.shtml), because it doesn't work with subsites/sub directories and relative links. You either send it to the root with relative pathing, or you use absolute... As a stop gap, I am using absolute.

    Had I stuck with relative it would send users from their intended destination, for example:

    [list]http://www.trinityportland.org/our-school/about/index.shtml[/list:u]
    to a broken link:

    [list]http://www.trinityportland.org/about/index.shtml[/list:u]
    So... what I need to do is redirect a user from all those folders on my site that hold content:

    [list]/about/index.shtml
    /assets/various media specific directories/file-name.ext
    /contact/
    /single/
    /academics/
    /admissions/
    /student-life/
    /alumni-parents/[/list:u]
    and have them redirect to the user requested file-name.ext but at the appropriate sub-directory:

    [list]/our-school/about/index.shtml
    /assets/various media specific directories/file-name.ext
    /our-school/contact/
    /our-school/single/
    /our-school/academics/
    /our-school/admissions/
    /our-school/student-life/
    /our-school/alumni-parents/[/list:u]

    This is sounding like a tall order, so even looking this concept over let alone actually providing a snippet sample that I can dupe for all the required directories would be amazing.

    Thanks again John et al, your help is greatly appreciated.
  • Try any of the following (make sure to do a hard refresh when testing each of these - shift + F5):

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*)\.shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION


    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 /(.*)\.shtml http://trinityportland.org/our-school/about/$1
    # END DIRECTORY REDIRECTION


    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 ^(.*)\.shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION


    If none of those work, try removing your cms's htaccess file in your site's root (at http://trinityportland.org/.htaccess) and see if it at least redirects correctly (ignore the 404).
  • Trying them now. When you say ignore the 404, what do you mean? Wouldn't that be a sign that it's not working (that's how I took that first snippet you provided)

    Also, I checked my root for an .htaccess at the onset, and there wasn't one there.

    The CMS is remotely hosted and just FTPs into the server to pull files up for editing (surrealcms.com), so it wouldn't create a .htaccess, I don't think... would it? (speaking out of uncertainty on that one, as I just don't know, but do know that there is no trinityportland.org/.htaccess file)
  • wait a second... wouldn't I want to put this .htaccess file in the trinityportland.org/ directory? (is that what you were saying? to overwrite whatever may be there...)

    Since the bad file request is going down past where .htaccess file is that has the rule for redirection... wouldn't my .htaccess file never have a chance to act on that request?

    Just trying to logic my way through something I don't quite fully grasp.
  • Seems, this thing is lopping off the extension as well as in most cases here not redirecting to the proper directory... that said, I understand what you meant about ignoring the 404s. The below are the results from using these three samples in the trinityportland.org/ as opposed to the trinityportland.org/our-school/ folder. When I used the /our-school/ folder... no redirection occurred - just 404s from borked links.

    this one is changing this url: http://trinityportland.org/about/index.shtml
    to this url: http://trinityportland.org/our-school/about/about/index

    with the following error report:
    The requested URL /our-school/about/about/index was not found on this server.

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*)\.shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION


    this one is changing this url: http://trinityportland.org/about/index.shtml
    to this url: http://trinityportland.org/our-school/a ... bout/index

    With the following error report:
    The requested URL /our-school/about//about/index was not found on this server.
    Additionally, a 301 Moved Permanently error was encountered while trying to use an ErrorDocument to handle the request.

    removing the portion in red: RedirectMatch 301 /(.*)\.shtml http://trinityportland.org/our-school/about/$1

    got me this error report: The requested URL /our-school/about/index was not found on this server.

    ** If I could stop this thing from nixing the extension, I think this may be the solution. **

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 /(.*)\.shtml http://trinityportland.org/our-school/about/$1
    # END DIRECTORY REDIRECTION


    this one is changing this url: http://trinityportland.org/about/index.shtml
    to this url: http://trinityportland.org/our-school/a ... ut/history

    With the following error report:
    The requested URL /our-school/about/about/history was not found on this server.

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 ^(.*)\.shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION
  • # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*)\.shtml http://trinityportland.org/our-school$1\.shtml
    # END DIRECTORY REDIRECTION
  • # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /
    RedirectMatch 301 /about/(.*)\.shtml http://trinityportland.org/our-school/about/$1\.shtml
    # END DIRECTORY REDIRECTION
  • Hey John, this one get's me:

    [list]url: http://trinityportland.org/about/index.shtml
    redirected url: http://trinityportland.org/our-school/a ... ////.shtml[/list:u]

    chrome's error is:
    The webpage at http://trinityportland.org/our-school/a ... c%5c.shtml has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

    ff's error is:
    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    We're so close...

    Another question for you though... once we get it for the /about/ directory, how would we go about implementing for the rest of them?

    MY guess - had this last one worked out:

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /
    RedirectMatch 301 /about/(.*)\.shtml http://trinityportland.org/our-school/about/$1\.shtml
    RedirectMatch 301 /contact/(.*)\.shtml http://trinityportland.org/our-school/contact/$1\.shtml
    RedirectMatch 301 /academics/(.*)\.shtml http://trinityportland.org/our-school/academics/$1\.shtml
    ... so on and so forth ...
    # END DIRECTORY REDIRECTION
  • I've kept tinkering... Oddly enough, I think that I just got what I asked for and it still didn't work?

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /
    RedirectMatch 301 /about/(.*)\.shtml http://trinityportland.org/our-school/about/$1.shtml
    # END DIRECTORY REDIRECTION


    [list]original url: http://trinityportland.org/about/index.shtml
    redirected url: http://trinityportland.org/our-school/about/index.shtml[/list:u]

    but I still get errors:

    Chrome:
    This webpage has a redirect loop.
    The webpage at http://trinityportland.org/our-school/about/index.shtml has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

    FF: 403
    You don't have permission to access /our-school/about/index.shtml on this server.
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    I don't understand these errors... When navigating the site, from the homepage, I get these same errors when clicking through to links that originally point to the correct file-path as well. Going directly to: trinityportland.org/our-school/about/index.shtml renders these same errors.
  • What's happening is /about/x.shtml matches with /our-school/about/x.html because it appears in the url (albeit at the end). This results in an infinite loop (or it would if apache didn't limit the number of redirects allowed).

    To restrict your pattern to the beginning of the url you use the beginning-of-line anchor (^). See if the following works:

    RedirectMatch 301 ^/about/(.*)\.shtml http://trinityportland.org/our-school/about/$1.shtml
  • Bingo.

    @john, @davidsgone... a debt of gratitude and enormous thanks here.

    My one final question is if I'm correct in thinking that the below will work to get to the other directories to redirect appropriately as we've accomplished here, or if I have to make a new RewriteBase, or even turn RewriteEngine on again for the others:

    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /
    RedirectMatch 301 ^/about/(.*)\.shtml http://trinityportland.org/our-school/about/$1.shtml
    RedirectMatch 301 ^/academics/(.*)\.shtml http://trinityportland.org/our-school/academics/$1.shtml
    RedirectMatch 301 ^/contact/(.*)\.shtml http://trinityportland.org/our-school/contact/$1.shtml
    # END DIRECTORY REDIRECTION

    a hundred thanks again gentlemen.
  • The answer here is yes.

    Adding the additional directories to additional RedirectMatch rules works...

    omg.

    I'm finally... finally, done with this site.

    Thanks again John and David, best to you both!

    viva-la-cssTricks!
  • Hie guys, I've an issue with the mod_rewrite, is about remove the whole example.com/index.php?page='home' to example.com/page/home i tried but stil with no success but only the index.php is gone there is my .htaccess look like:

    Options +FollowSymLinks RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?page=$1

    RewriteRule ^(([^/]+/)*)index.php?$ http://www.example.com/$1 [R=301,L]