Solved

mod_rewrite questions

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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