LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 02:42 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

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, 03:40 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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, 06:45 AM   #3
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

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 06:49 AM.
 
Old 07-25-2012, 07:41 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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, 07:54 AM   #5
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

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, 08:31 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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, 12:27 PM   #7
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

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 12:28 PM.
 
  


Reply

Tags
apache2, mod_rewrite



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 06:02 AM
unusual apache mod_rewrite rule entz Linux - Server 1 10-04-2009 09:48 AM
Apache2 mod_rewrite rule for changing file extentions CyberDog3K Linux - Server 0 08-27-2007 01:25 AM
mod_rewrite - rule to change http to https Firebar Linux - Server 10 06-05-2007 07:29 AM
mod_rewrite rule question jmoschetti45 Linux - Software 0 03-19-2007 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:58 PM.

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
Open Source Consulting | Domain Registration