Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] .htaccess mod_rewrite

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #28135
    copperSmith
    Member

    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,

    #71526
    john010117
    Member
    Code:
    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.

    #71527
    copperSmith
    Member

    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,

    #71533
    copperSmith
    Member

    dratts… this doesn’t seem to work. Here is what I have:

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

    Things I changed:

      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:

    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:

      /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:

      /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.

    #71535
    john010117
    Member

    Try any of the following (make sure to do a hard refresh when testing each of these – shift + F5):

    Code:
    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*).shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION
    Code:
    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 /(.*).shtml http://trinityportland.org/our-school/about/$1
    # END DIRECTORY REDIRECTION
    Code:
    # 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).

    #71538
    copperSmith
    Member

    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)

    #71539
    copperSmith
    Member

    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.

    #71540
    copperSmith
    Member

    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.

    Code:
    # 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. **

    Code:
    # 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.

    Code:
    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 ^(.*).shtml http://trinityportland.org/our-school/about$1
    # END DIRECTORY REDIRECTION
    #71554
    john010117
    Member
    Code:
    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /about/
    RedirectMatch 301 (.*).shtml http://trinityportland.org/our-school$1.shtml
    # END DIRECTORY REDIRECTION
    #71555
    copperSmith
    Member

    :) .htaccess… oh can’t I just write you in english….

    This: http://www.trinityportland.org/our-scho … ndex.shtml

    Went to this: http://trinityportland.org/our-school/o … C%5C.shtml

    #71557
    john010117
    Member
    Code:
    # BEGIN DIRECTORY REDIRECTION
    RewriteEngine on
    RewriteBase /
    RedirectMatch 301 /about/(.*).shtml http://trinityportland.org/our-school/about/$1.shtml
    # END DIRECTORY REDIRECTION
    #71630
    copperSmith
    Member

    Hey John, this one get’s me:

    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:

    Code:
    # 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
    #71631
    copperSmith
    Member

    I’ve kept tinkering… Oddly enough, I think that I just got what I asked for and it still didn’t work?

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

    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.

    #71640

    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:

    Code:
    RedirectMatch 301 ^/about/(.*).shtml http://trinityportland.org/our-school/about/$1.shtml
    #71681
    copperSmith
    Member

    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:

    Code:
    # 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.

Viewing 15 posts - 1 through 15 (of 17 total)
  • The forum ‘Back End’ is closed to new topics and replies.