SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: Mod Rewrite

  1. #1
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Mod Rewrite

    This is driving me insane. I am trying to use mod rewrite to get from the url:

    www.mysite.com/folder/?param=1

    to:

    www.mysite.com/folder/1

    I have tried out about 10 different mod rewrite version from varios forums but nothing is working. I just keep on getting an Internal Server Error message. Below is one example i have tried but for the life on me can see the problem. Is this code correct? If it is any other ideas what might be causing the issue?

    Thanks!

    RewriteEngine On

    RewriteBase /
    RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [NC]
    Last edited by ServerStorm; Sep 10, 2012 at 08:57. Reason: added [noparse][/noparse] tags to the urls

  2. #2
    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)
    BM,

    1. You don't need the RewriteBase.

    2. You don't need (can't have) the leading / with an Apache 2.x server (it's required for an Apache 1.x server, though).

    3. No Case is NOT appropriate for a RewriteRule (the {REQUEST_URI} variable IS case sensitive).

    4. Make those mods then test with the flag(s) being [R=301,L] and remove the R=301, when you're satisfied it works.

    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

  3. #3
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi dklynn,

    Thanks for the reply! I have tested what you recommended but im still getting the same error. I have tried the following all with the same result:

    RewriteEngine On

    RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L]

    RewriteRule ^/folder/(.*)$ folder/index.php?param=$1 [R=301,L]

    RewriteRule ^folder/(.*)$ folder/index.php?param=$1 [R=301,L]




    I have other instructions in my .htaccess so maybe they are conflicting somehow?

    # redirect non www. to www. domain

    RewriteCond %{HTTP_HOST} ^mysite.com
    RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]


    # Remove the file extension from URL's

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L,QSA]


    # server automatically adds a PHP Session ID to URL's so this needs to be switched off

    php_flag session.use_trans_sid off
    php_flag session.use_only_cookies on

  4. #4
    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)
    BM,

    (Not so) silly question first: Is mod_rewrite enabled? (Several ways to check and verify in my signature's tutorial.)

    Your "other" mod_rewrite code is fine.

    I've got to assume that you tried your new code (after the other mod_rewrite code) individually:

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

    Code:
    RewriteEngine On
    
    RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L]
    # This will ONLY work on an Apache 1.x server (^/) and
    # ^ will only work on an Apache 2.x server.
    # If you're not sure what you're using, use ^/? as that works with both.
    
    # the / in the redirection is first interpreted as "server root" but should be okay.
    
    # (.*) is a terrible thing to use: 
    
    [standard rant #1]
    The use of "lazy regex," specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/standard rant #1] # In other words, use the !-f test to prevent looping on folder/index.php!
    RewriteRule ^/folder/(.*)$ folder/index.php?param=$1 [R=301,L] # same ^/ problems (#1 and #3) as above RewriteRule ^folder/(.*)$ folder/index.php?param=$1 [R=301,L] # Same loopy problem (#3 above).
    Code:
    # redirect non www. to www. domain
    
    RewriteCond %{HTTP_HOST} ^mysite.com
    RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
    # I prefer to use existing variables rather than create new ones so I use
    # RewriteRule .? http://www.mysite.com%{REQUEST_URI} [R=301,L]
    # but your RewriteRule is the more common use (by noobies)
    
    # Remove the file extension from URL's
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L,QSA]
    # I will check whether $1.php exists as a file before making this redirection
    
    # server automatically adds a PHP Session ID to URL's so this needs to be switched off
    
    php_flag session.use_trans_sid off
    php_flag session.use_only_cookies on
    Technique tip: Because Apache's core directives are executed before mod_rewrite, I will always list them first.

    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

  5. #5
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi dklynn,

    I dont understand what you mean when you say to use the !-f test to prevent looping on folder/index.php?

    From your notes above i gather that the below should work then on apache 1 and 2 server, but i should change the (.*)?

    [code]RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L][\code]

    I have tried about 6 different .htaccess code generators all with no luck. There is clearly something fundamental i am doing wrong here but i still cant figure out what?

  6. #6
    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)
    BM,

    If mod_rewrite enabled? Have you confirmed that (and that it works on a simple test)? EVERYTHING else would be a waste of time if it's not enabled for you.

    Quote Originally Posted by Banana Man View Post
    I dont understand what you mean when you say to use the !-f test to prevent looping on folder/index.php?

    That's simply one way to escape from the loop you're generating with the inappropriate use of (.*), i.e., test whether the file exists before redirecting back to it with your LOOPY code.

    [standard rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/standard rant #1]

    From your notes above i gather that the below should work then on apache 1 and 2 server, but i should change the (.*)?

    WRONG! In the RewriteRule, the slash is required (in the DocumentRoot) by Apache 1.x servers following the start anchor, i.e., ^/... Apache 2.x assumes that there is a / following the domain name so it will NOT match ^/, it will only match ^. If you don't know which version of Apache you're using (then shame on you), you can use ^/? as that's accepted by both versions of Apache.

    [code]RewriteRule ^/folder/(.*)$ /folder/index.php?param=$1 [R=301,L][\code]

    As explained above, there are two problems with this (if using Apache 2.x): The leading / and the simple fact that (.*) will match the redirection (index.php) and loop. Since you're not locking up the server with your loop, you can assume that you're using an Apache 2.x server (it'll make 10 attempts before giving up but it won't lock-up the server and require a restart).

    I have tried about 6 different .htaccess code generators all with no luck. There is clearly something fundamental i am doing wrong here but i still cant figure out what?

    Forgive me for stating the obvious: If you don't know what you're doing, it's likely that you're not inputting good information to the code generators (which MUST make some assumptions - I know! I created a mod_rewrite generator and it's not a trivial exercise). It would be far simpler for you to spend a few minutes with a tutorial (like the one linked below) which lays it all out for you and has helped many members learn about mod_rewrite over the several years it's been online.
    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 Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I checked my phpinfo() file but mod_rewrite wasn't listed. I have been in contact with my hosting company and they assure me that even though it is not listed in my phpinfo() it is turned on on my server.

  8. #8
    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)
    BM,

    Did you run the test with the code I provided in the tutorial? That will give you a definitive answer.

    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

  9. #9
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I tested it and it is working ok. I see the "this is a PHP file" text.

    I was wondering is it ok to have the .htaccess file save at my root folder or should it be in the /folder/ as that is what i am trying to effect? I have tried both but not with every test i have tried.

  10. #10
    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)
    BM,

    Well, that's as good a confirmation as you can get!

    As a rule, I keep my .htaccess in the DocumentRoot so I can see all the interactions (conflicts) at a single glance. You CAN do either but that will complicate things unnecessarily for you.

    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

  11. #11
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So i finally got it working. I am using the following:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ /folder/index.php?param1=$1&param2=$2
    which converts:
    Code:
    www.mysite.com/folder/index.php?param1=test&param2=1
    to:
    Code:
    www.mysite.com/test/1/
    The problem was that i had this code beforehand which must have been causing some kind of conflict:

    Code:
    # Remove the file extension from URL's
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L,QSA]

  12. #12
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Off Topic:


    When posting .htaccess files, order matters! So if you have a block A and a block B in your .htaccess file it is wise to also post in which order they are in your .htaccess file.
    In this particular post that would have made the problem clear after post #3 (where the OP posts two blocks but it turns out the second one actually came before the first one he posted)

    This is not to attack anyone, just to telling the OP and others a way we can better help them
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  13. #13
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Another day another .htaccess problem

    I now have a new problem with this code. If i try to navigate to www.mysite.com/folder2/ i.e. droping the index.php at the end i get a server error. It works if i use the full address i.e. www.mysite.com/folder2/index.php. It seems to be trying to use the new mod rewrite rule i just added even though a different folder has been specified. Any ideas why this is happening?

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    
    # redirect non www. to www. domain
    
    RewriteCond %{HTTP_HOST} ^mysite.com
    RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
    
    
    # server automatically adds a PHP Session ID to URL's so this needs to be switched off
    
    php_flag session.use_trans_sid off
    php_flag session.use_only_cookies on
    
    
    # remove the variables when passed through domain URL
    
    RewriteRule (.*)-(.*)/$ /folder1/index.php?param1=$1&param2=$2
    
    
    # Remove the file extension from URL's
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L,QSA]
    Thanks!

  14. #14
    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)
    BM,

    [rant #1 REPEATED]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/rant #1]

    Sorry, you are NOT specifying a folder with (.*) but matching EVERYTHING (or NOTHING). That's been the problem and it's still the problem.

    Once you finally get into your folder2 directory, DirectoryIndex index.php will specify that index.php will be served if only the directory is requested.

    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

  15. #15
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks again! I'm new to Regex as well so it was all going over my head a bit. I have looked through the regex tutorial and got it working.

  16. #16
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Newbie here, trying to map old urls to new urls using rewriterules. I've converted a website from DNN to Joomla, and now I'm trying to get my 301 redirects in place before rolling out the new site. I have 7 templates, each which are applied to 22 models. For example, I have:

    http://www.mywebsite.com/long_compli...es_here_1.aspx
    http://www.mywebsite.com/long_compli...section_a_goes here.aspx
    http://www.mywebsite.com/long_compli...section_b_goes here.aspx
    http://www.mywebsite.com/long_compli...section_c_goes here.aspx
    http://www.mywebsite.com/long_compli...section_d_goes here.aspx
    http://www.mywebsite.com/long_compli...section_e_goes here.aspx
    http://www.mywebsite.com/long_compli...section_f_goes here.aspx


    So each "long_complicated_subject" has 7 pages (landing page and 6 subpages). The subpages for every long_complicated_subject has the same name.... like this:

    http://www.mywebsite.com/long_compli...es_here_1.aspx
    http://www.mywebsite.com/long_compli...section_a_goes here.aspx
    http://www.mywebsite.com/long_compli...section_b_goes here.aspx
    http://www.mywebsite.com/long_compli...section_c_goes here.aspx
    http://www.mywebsite.com/long_compli...section_d_goes here.aspx
    http://www.mywebsite.com/long_compli...section_e_goes here.aspx
    http://www.mywebsite.com/long_compli...section_f_goes here.aspx

    http://www.mywebsite.com/long_compli...es_here_2.aspx
    http://www.mywebsite.com/long_compli...section_a_goes here.aspx
    http://www.mywebsite.com/long_compli...section_b_goes here.aspx
    http://www.mywebsite.com/long_compli...section_c_goes here.aspx
    http://www.mywebsite.com/long_compli...section_d_goes here.aspx
    http://www.mywebsite.com/long_compli...section_e_goes here.aspx
    http://www.mywebsite.com/long_compli...section_f_goes here.aspx

    and so on for many "long_complicated_subject" groups. As you can see each long_complicated_subject" has a landing page, and 6 subpages with identical names to every other "long_complicated_subject". There is no consistency to the actual "long_complicated_subject", but I'm hoping I don't have to duplicate the subsections for every "long_complicated_subject".

    Instead of a one-to-one mapping, and ending up with hundreds of rewriterule lines, is there a way to do this in a 2-step process; that is, may I first substitute the "long_complicated_subject" and the once that's completed, may I do a second substitution on result of the first one?

    One more thing, is there a way report the original url and the final url with a 301 back to the search engines?

    FYI --- the domain name is not changing.

    Thanks,
    Dan

  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)
    BM,



    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
  •