LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache mod_rewrite rule help (https://www.linuxquestions.org/questions/linux-server-73/apache-mod_rewrite-rule-help-4175418538/)

the_gripmaster 07-25-2012 02:42 AM

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?

bathory 07-25-2012 03:40 AM

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

the_gripmaster 07-25-2012 06:45 AM

Quote:

Originally Posted by bathory (Post 4737442)
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?

bathory 07-25-2012 07:41 AM

Quote:

Originally Posted by the_gripmaster (Post 4737571)
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.

the_gripmaster 07-25-2012 07:54 AM

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

bathory 07-25-2012 08:31 AM

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>


the_gripmaster 07-25-2012 12:27 PM

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.


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