Forums

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

Home Forums Other How to remove subdirectories using mod_rewrite in .htaccess

  • This topic is empty.
Viewing 11 posts - 16 through 26 (of 26 total)
  • Author
    Posts
  • #171072
    ewisely
    Participant

    Use REQUEST_URI when you want to compare a pattern to the URI. Use REQUEST_FILENAME when you want the filename.

    Think I slightly get what you mean about URI and FILENAME. I’ll definitely need more time to explore the use of these 2 requests in order to understand more on how different they behave in the process of mod-rewrite. Thanks for that.

    (if your routine/ files will always have a dash in the name, and there will never be other files with dashes in the name, then you might be able to write a rule to recognize that. But that’s a big “if,” and I would not recommend this approach.)

    I take your advice. I don’t want to get complicated with this mod_rewrite thing too since I just started to learn it 10 days ago. By the way, I saw this:

    RewriteRule ^([a-zA-Z_-\s]+)/$ /$1.html
    

    What does that _-\s do?

    #171073
    __
    Participant

    Adds an underscore (_), dash (-), and whitespace (\s matches any of space, horizontal tab, form feed, newline, carriage return)* to the character class.

    * (I don’t see any reason to add \s, however, since whitespace is forbidden in URLs.)

    #171075
    ewisely
    Participant

    Hi traq,

    Understood. Thank you very very much for your great patience and help. I’ve learned a lot from you. :D

    #171090
    __
    Participant

    No problem, glad you’re learning.

    #176637
    ewisely
    Participant

    Hi traq,

    I’m having problem doing 301 redirects. As I have just changed my site’s url structure from:

    mysite.com/q/sleep-rest.htm to mysite.com/sleep-rest/
    mysite.com/q/play-study.htm to mysite.com/play-study/
    and so on…

    I need to redirect them all with just a single line of code. So, I do this:

    RewriteRule ^q/([a-zA-Z0-9_-]+)\.htm$ /$1/ [R=301,L]
    <code></code>

    But I got a “redirect loop” error. What’s wrong with my code? Appreciate your help. Thanks.

    #176713
    __
    Participant

    I’m not sure exactly what is causing it, but a “redirect loop” is where you have rewrite rules that create a URL that matches (the same or another) rewrite rule, so the URL is rewritten constantly, without end.

    Is that the only rewrite rule you’re using?

    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

    To address your problem, you have a file at (for example):

    /q/sleep-rest.htm
    

    and you want to use the following URL to access it:

    mysite.com/sleep-rest/
    

    Correct?

    RewriteRule ^/([a-zA-Z0-9_-]+)/?$ /q/$1.htm
    
    #176721
    ewisely
    Participant

    Sorry for not being clear.

    My site url (under /q/) used to be like that:

    mysite.com/q/sleep-rest.htm
    mysite.com/q/play-study.htm
    and so on…

    I’ve used RewriteRule ^/([a-zA-Z0-9_-]+)/?$ /q/$1.htm so that when people click on (or type into the browser) mysite.com/sleep-rest/ or mysite.com/sleep-rest, both will retrieve content from mysite.com/q/sleep-rest.htm

    However, because this site is pretty old and has quite a number of old urls pointing back from other websites. So, I want to do a permanent redirect of mysite.com/q/sleep-rest.htm to mysite.com/sleep-rest/and likewise for the rest of the urls to avoid creating confusion to my visitors.

    Since my files are all under /q/ folder, I’m trying to use a single line of code (RewriteRule ^q/([a-zA-Z0-9_-]+)\.htm$ /$1/ [R=301,L]) to perform the 301 redirect for all old urls but it invokes a redirect loop.

    Is it actually possible to work out something like that? Appreciate your help.

    #176725
    __
    Participant

    Ahh… so, you’ve got one rule converting from form A→B, and then another converting from form B→A. See the infinite loop?

    Instead of trying to match the legacy URL, use a rewrite condition to check against the actual request, so you only rewrite it when needed:

    RewriteCond %{THE_REQUEST} /q/([\w-]+)\.htm
    RewriteRule ^.*$ /%1 [R=301]
    
    # follow with other rules as normal
    RewriteRule ^([\w-]+)/?$ /q/$1.htm
    
    #176731
    ewisely
    Participant

    The code works like magic!!

    
      RewriteCond %{THE_REQUEST} /q/([\w-]+)\.htm
      RewriteRule ^.*$ /%1 [R=301]
    

    But could you explain a little more about what the code does, so that I can learn how to use %{THE_REQUEST}, \w- and /%1 which look totally Greek to me. Thank you very much. :)

    #176878
    __
    Participant

    %{THE_REQUEST} is an Apache variable. Its value is the actual request line* from the HTTP request, unchanged by any rewrites (which is the reason it does not cause a redirect loop).

    * for example:

    GET /index.html HTTP/1.1
    

    \w- is actually - (a dash) and \w (a “word” character; meaning alphanumerics or an underscore). Put together, [\w-] is just a shorter, more succinct way to write [a-zA-Z0-9_-].

    %1 is a numbered backreference, just like $1 would be. The difference is that % backreferences refer to capture groups in RewriteConds, while $ backreferences refer to capture groups in RewriteRules.

    #176886
    ewisely
    Participant

    Thanks for your clear explanation and reference. I really appreciate your passion in helping people like me. Thank you very very much. Have a nice day every day. :)

Viewing 11 posts - 16 through 26 (of 26 total)
  • The forum ‘Other’ is closed to new topics and replies.