LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 07-25-2012, 03:42 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 362

Rep: Reputation: 38
Question Apache mod_rewrite rule help


I am trying to write a number of apache rewrite rules, seems like the regex are not matching

This is what needs to be done


Code:
OLD -> NEW

http://www.example.com/source/aus-nz -> http://www.example.com/source-code/australia-nz
http://www.example.com/source/aus-nz/1.txt -> http://www.example.com/source-code/australia-nz/1.txt
http://www.example.com/source/aus-nz/2.txt -> http://www.example.com/source-code/australia-nz/2.txt
And these are the rewrite rules I am using (mod_rewrite is enabled in apache)

Code:
RewriteEngine On

RewriteRule ^/source/aus-nz/(.*) /source-code/australia-nz/$1
Am I doing this right?
 
Old 07-25-2012, 04:40 AM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,973

Rep: Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343
Hi,

You don't need the starting and the trailing slashes in the URL that's to be rewritten. So try this:
Code:
RewriteEngine On

RewriteRule ^source/aus-nz(.*) /source-code/australia-nz$1
Regards
 
1 members found this post helpful.
Old 07-25-2012, 07:45 AM   #3
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 362

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by bathory View Post
Hi,

You don't need the starting and the trailing slashes in the URL that's to be rewritten. So try this:
Code:
RewriteEngine On

RewriteRule ^source/aus-nz(.*) /source-code/australia-nz$1
Regards
Not working

I forgot to mention, it is a virtualhost, would that matter?

Last edited by the_gripmaster; 07-25-2012 at 07:49 AM.
 
Old 07-25-2012, 08:41 AM   #4
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,973

Rep: Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343
Quote:
Originally Posted by the_gripmaster View Post
Not working

I forgot to mention, it is a virtualhost, would that matter?
Please define "Not working".
Did you clear your browser cache? Are you using the rewrite stuff from a .htaccess file, or from the apache config file?
And it doesn't matter if it's a virtual host of not.
 
Old 07-25-2012, 08:54 AM   #5
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 362

Original Poster
Rep: Reputation: 38
Not working =
http://www.example.com/source/aus-nz not changing to http://www.example.com/source/aus-nz
The rewrite rules are not taking any effect.


Browser cache? Cleared (also using Ctrl+F5 to force a proper refresh)

Rewrite rules are in the apache config file
 
Old 07-25-2012, 09:31 AM   #6
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,973

Rep: Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343
Quote:
Rewrite rules are in the apache config file
In this case you need to reload apache.
And since it's a vhost, you need to put those directives inside the vhost's definition section inside the <Directory ...>...</Directory> where you define the DocumentRoot directory of the vhost. E.g.
Code:
<VirtualHost *:80>
Servername www.foo.com
DocumentRoot /var/www/html/foo
<Directory /var/www/html/foo>
RewriteEngine on
RewriteRule ^source/aus-nz(.*) /source-code/australia-nz$1
</Directory>
...
</VirtualHost>
 
Old 07-25-2012, 01:27 PM   #7
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 362

Original Poster
Rep: Reputation: 38
Question

I finally did it. Our setup is a bit complicated. The requests to the set of apache hosts come through a hardware load balancer depending on the URL, the this rewrite again passes the requests to the LB which again forwards it to another set of apache hosts based on the URL. And to complicate matters there is a third party apache module. This is what I ended up with

Code:
RewriteRule ^/source/aus-nz(.*) http://www.example.com/source-code/australia-nz$1 [R,L]
Thanks for your replies.

Last edited by the_gripmaster; 07-25-2012 at 01:28 PM.
 
  


Reply

Tags
apache2, mod_rewrite


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mod_rewrite rule from window, now in linux not working kingkashif Linux - General 7 11-20-2009 07:02 AM
unusual apache mod_rewrite rule entz Linux - Server 1 10-04-2009 10:48 AM
Apache2 mod_rewrite rule for changing file extentions CyberDog3K Linux - Server 0 08-27-2007 02:25 AM
mod_rewrite - rule to change http to https Firebar Linux - Server 10 06-05-2007 08:29 AM
mod_rewrite rule question jmoschetti45 Linux - Software 0 03-19-2007 08:29 PM


All times are GMT -5. The time now is 08:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration