Skip to main content

.htaccess and params


i digg on the internet about .htaccess and rewrite rules i need to do with my site.



i saw something i dont understand and want to know what it means



i am wondering what is the difference between the 2 regular expression that i need to use for my site among all others i need to use):




RewriteRule ^home$ mainpage.php?id=$1 [QSA]



and




RewriteRule ^home(/)?$ mainpage.php?id=$1 [L]



i looked at the QSA and L but what the (/) means?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. The expression will be (/)? not (/). It means slash or not on the URL. These two URLs will go to the same place:

    http://www.domain.com/home/

    and

    http://www.domain.com/home


    So the slash is optional. This way, if a bot or a search engine puts it, the rule will work.

    You can rewrite the rules like this:

    RewriteRule ^home/?$ mainpage.php?id=$1 [L,QSA]


    Also, I saw you said:


    among all others i need to use


    If all your pages goes to the same file (in this case mainpage.php), you can create one rule that will automatically rewrites them instead of creating 10 or 15 rules (or more). You can do this like this:

    RewriteEngine on
    REwriteBase /
    RewriteRule ^([a-z0-9\-_]+)/?$ mainpage.php?id=$1 [L,QSA]


    This rule will use Letters, Numbers, Dash and Underscores as the page.

    ReplyDelete
  2. (/)? means "an optional slash".


    (/) creates a group containing/matching whatever's between the parentheses. In this case, since it's a single character, the parentheses are more for capturing pieces than anything else. (Whatever matches will become $1, $2, etc depending on how many capture groups (sets of parentheses) there are in the pattern and where they are.)


    BTW, your first RewriteRule doesn't make a whole lot of sense -- since there's no group to provide a value for $1, it'll probably always be blank. In the second, $1 will either be empty or a slash, depending on whether the subpattern matches. home will be rewritten to mainpage.php?id=, and home/ will be rewritten to mainpage.php?id=/.


    The ? means "0 or 1 of the previous atom". ("Atoms", BTW, are the building blocks of a regex.) In this case, since the atom preceding it is (/) (which is effectively /), the ? means "0 or 1 slashes".

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.