Solved

mod_rewrite questions

Posted on 2004-10-06
3
388 Views
Last Modified: 2010-03-04
I wanted to do the two following things with mod_rewrite (or any other mod if possible):

1) Prevent hotlinking
2) Resolve relative URL's (such as /articles/9/ would actually read articles.php?ID=9)

Thanks in advance.
0
Comment
Question by:drakkarnoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Author Comment

by:drakkarnoir
ID: 12271925
So nobody knows how to do this?
0
 
LVL 4

Expert Comment

by:kalosi
ID: 12287013
Hi,

the second point could be done like this

RewriteEngine On

RewriteRule ^(.+)/$ /$1   ## remove trailing slash
RewriteRule ^/articles/(.*)$    /article.php?id=$1  [R,L]    ## redirect to the URL and stop the processing


david
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12447002
Hotlinking prevention, see: http://www.htmlite.com/HTA013.php

Dynamic - Static URL's:

First of all take a look at these links, they show the basics of mod rewriting very well:

http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
http://www.4webhelp.net/tutorials/misc/mod_rewrite.php

The basic steps are this:

1) Create a .htaccess file.

2) Add this line:

RewriteEngine on

This effectively initiates the Apache module used for the mod rewriting.

3) Now for the rewrite rules, the syntax is as follows:

RewriteRule The_Pattern The_Replacement

The pattern and the replacement are often based on regular expressions so if you have no previous knowledge of regex now would be a great time to brush up on your knowledge.

A very brief overview of meta characters you will more likely need:

^  beginning of item/line
$  end of item/line
\  used to escape certain characters
.  matches any character (but not new line)
[]  used to define accepted character classes
|  "or" - or an alterantive
()  grouped pattern
*  0 or more
+  1 or more
{}  min/max quantity

Here is a few examples of how you can use mod rewriting:

RewriteRule /articles/([0-9]+) /articles.php?id=$1

/articles/2   >>>   /articles?id=2

RewriteRule /category/([a-z]+)/([0-9]+) /category.php?action=$1&cat=$2

/category/show/3   >>>   /category.php?action=show&cat=3

RewriteRule ^cat_([0-9]+)_([0-9]+).html$ category.php?cat=$1&page=$2

cat_1_3.html   >>>   category.php?cat=1&page=3
0

Featured Post

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question