Jump to content

Your Best Uses


Recommended Posts

Ha! Actually wanted to be the first to post here.  8) However, I do actually have a point. I'm wondering what your handiest uses are for mod_rewrite. I'm a newb to the module, but have been able to use it in a limited way. One of the things I am most proud of is redirecting traffic to the secure port (443) for certain virtual hosts. Something like this:


    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


I was wondering though if anyone could tell me what the flags (NC, R, and L) mean after the RewriteRule.
Link to comment
Share on other sites

These flags are explained in the [url=http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule]Apache mod_rewrite[/url] documentation. Scroll down until you find the following paragraph:
[quote]Additionally you can set special flags for Substitution by appending [flags]  as the third argument to the RewriteRule  directive. Flags is a comma-separated list of any of the following flags:[/quote]
Below that it explains what the flags mean, along with other flags too.

But here is their names:
[L] = Last Rule
[NC] = No Case
[R] = Force Redirect.

More info can be found on these by going to the link above.
Link to comment
Share on other sites

I've made a Myspace URL type script where users can have their own URL on the site (site.com/name) and it will then be forwarded to the profile page.

[code]RewriteEngine  on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?moduser=$1 [L][/code]

In my database I have a moduser field where the script searches, then picks up the userid, then uses the id for the rest of the page.

Script in profile.php
[code]include "connect.php";
  global $userid, $moduser;
$query = "SELECT * FROM users WHERE moduser = \"$moduser\"";
$result = mysql_query($query) or die('Query mod failed: ' . mysql_error());
$countmod = mysql_numrows($result);
$userid = mysql_result($result, 0, 'userid');
if($countmod > 0){
  $query2 = "SELECT * FROM users WHERE userid = $userid";
  $result2 = mysql_query($query2) or die('Query id failed: ' . mysql_error());
  $name = mysql_result($result2, 0, 'name');
  $name = stripslashes($name);
  $name = $name." on ";
  $query2 = "SELECT * FROM users WHERE userid = $userid";
  $result2 = mysql_query($query2) or die('Query id failed: ' . mysql_error());
  $name = mysql_result($result2, 0, 'name');
  $name = stripslashes($name);
  $name = $name." on ";

**Don't really pay attention to $name, i just use that as a prefix for the title of the page later on.

Link to comment
Share on other sites

I have made a tutorial page with easy to remember urls, that are the categories it is under followed by the tutorial name -- see http://monkeymatt.fusionxhost.com/tutorials/

Here is the appliciable .htaccess code
RewriteRule ^tutorials/$ tutorials.php?cat_name=Main
RewriteRule ^tutorials/((([^/]+)/)+)$ tutorials.php?cat_name=$1
RewriteRule ^tutorials/((([^/]+)/)+)([^/]+)/examples/([0-9]+)$ tutorials.php?cat_name=$1&tutorial_name=$4&example=$5
RewriteRule ^tutorials/((([^/]+)/)*)([^/]+)/([0-9]+)$ tutorials.php?cat_name=$1&tutorial_name=$4&page=$5
RewriteRule ^tutorials/((([^/]+)/)*)([^/]+)$ tutorials.php?cat_name=$1&tutorial_name=$4&page=1

The PHP code then takes the cat names, separates them, and finds the cat id (using all of the cat names allows for categories with same names, just not in the same category). Then it loads the necessary stuff using the names.

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.