Mod_Rewrite Condition to Redirect External Referrers

Posted on 2014-11-21
Last Modified: 2014-11-22
We inflate   EPUB's on our web site in advance, save them to e.g.


We then use LiveCode - RevIgniter to serve these inside an iFrame, inside our site wrapper.. this works great.

BUT  the  problem is Google is finding these pages, indexing and caching them and they are linked to and delivered outside our site wrapper. e.g.

I need to create a mod_rewrite condition that will redirect these to our CMS

in pseudo-code it would look like

( RewriteCond $1 ^(media/books/[any string here, some reg ex I presume]/web/ops)  ) AND (referrer IS NOT localhost)
RewriteRule ^(.*)$$1 [L]  

can you help me with the proper mod_rewrite rule for this?

Question by:Brahmanatha
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
  • 4
  • 2
LVL 51

Expert Comment

by:Steve Bink
ID: 40459537
Your pseudo code is very similar to your actual need:
RewriteCond %{HTTP_REFERER} !^localhost$ [NC]
RewriteRule ^/?(media/books/(some regex)/web/ops) /$1 [L,NC]

Open in new window

You'll need to be more explicit about (some regex), but that is the general form you want.

Author Comment

ID: 40459818
Interesting... very simple really.  

"be more explicit about (some regex)" right...

The string ##-###-### that forms the URL segment between "books" and "web"

is a single string with no slashes (no additional segments)

/media/books/  ##-###-## /web/ops

where  #######is the title of a book. The strict convention on our site is: all lower case letters, words words delimited by dashes, underscore at the end followed by ISO 2 ltr language code where not English  e.g.

dancing-with-siva_es # in spanish

Some works in other languages have their titles in those languages, but the convention is that these strings ("file_id" in our mySql database) are always in the 0-127 range (ASCII) no unicode and no ANSI roman chars such as umlauts etc.  have trouble with figuring out how to make my reEx from being too "greedy") in needs to stop at the next slash in the URL string. How's this for an attempt?

RewriteRule ^/?(media/books/(.*?)/web/ops) /$1 [L,NC]

or possibly more explicit?

RewriteRule ^/?(media/books/(.*\/)web/ops) /$1 [L,NC]

Where the regex ends at the next slash?
LVL 51

Accepted Solution

Steve Bink earned 500 total points
ID: 40460131
You're on the right track, but let me see if I can clarify your requirements...

>>> The string ##-###-### that forms the URL segment between "books" and "web"
>>> is a single string with no slashes (no additional segments)

No problem there.  Detection pattern would be /books\/([^/]+)\/web/

>>> The strict convention on our site is: all lower case letters, words words delimited
>>> by dashes, underscore at the end followed by ISO 2 ltr language code where not English  e.g.

Even better..  /books\/([-a-z_]+)\/web/

The final question is, assuming there is a match, do you want the whole URL, or just the matched title?
# for the whole URL
RewriteRule ^/?(media\/books\/[-a-z_]+\/web/ops) /$1 [L,NC]

# for just the title
RewriteRule ^/?media\/books\/([-a-z_]+)\/web/ops) /$1 [L,NC]

Open in new window

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.


Author Comment

ID: 40460273
the whole URL... our controller will need to see "/media" at beginning plus the matched title and the final segment, which is the page in the book to target. From there we will use another model/library script to display the page in our views.

Thanks for your help!

Author Comment

ID: 40460288
Hmmm I had to extend this a bit:

RewriteCond %{HTTP_REFERER} !^localhost$ [NC]
RewriteRule ^/?(media\/books\/[-a-z_]+\/web\/ops\/xhtml\/.*) /$1 [L,NC]

and now it works! at least our CMS gets the URL...

Author Closing Comment

ID: 40460289
Thanks Steve!

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

726 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