Jump to content

Archived

This topic is now archived and is closed to further replies.

R_P

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:

[code]

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

[/code]

I was wondering though if anyone could tell me what the flags (NC, R, and L) mean after the RewriteRule.

Share this post


Link to post
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.

Share this post


Link to post
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.

.htaccess
[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;
 
  if(isset($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 ";
  }else{
  $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 ";
  }[/code]

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

-Chris

Share this post


Link to post
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
[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
[/code]

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.

Monkeymatt

Share this post


Link to post
Share on other sites

×

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.