Solved

mod_rewrite questions

Posted on 2004-10-06
3
350 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
3 Comments
 

Author Comment

by:drakkarnoir
Comment Utility
So nobody knows how to do this?
0
 
LVL 4

Expert Comment

by:kalosi
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now