LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-05-2011, 02:36 PM   #1
PlymWS
Member
 
Registered: Aug 2006
Posts: 51

Rep: Reputation: 16
Rewriting a URL with mod_rewrite


I'm trying to do a simple URL rewrite of a URL to pass it through a script but the .htaccess I've written doesn't work.

I'm trying to rewrite something like this:

http://sub.domain.com/result.xml

so that the server sees:

http://sub.domain.com/script.php/result.xml

so that when someone asks for result.xml it goes through a parsing script (script.php) first.

What I've got so far from my Google searching is this:

Code:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.xml*$ script.php/$1
but this doesn't pass through .xml. I've not read something correctly so how would I achieve this ?
 
Old 07-05-2011, 04:24 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,167
Blog Entries: 1

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Hi,

Try this:
Code:
Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/([^/]+)\.xml
RewriteRule ^(.*)\.xml$ script.php/$1.xml [L]
Regards
 
Old 07-05-2011, 10:10 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I think PlymWS rule gets into an infinite rewrite loop (/script.php/script.php/script.php/.../result.xml).

bathory's result should work well for .xml files in the root, but it will not redirect say /something/result.xml to /script.php/something/result.xml. I think it is equivalent to
Code:
Options +FollowSymlinks
RewriteEngine on

RewriteRule  ^/+([^/]*\.xml)$  /script.php/$1  [L]
If you need the redirection for .xml files in subdirectories too, try
Code:
Options +FollowSymlinks
RewriteEngine on

RewriteCond  %{REQUEST_URI}  !^/+script\.php/
RewriteRule  ^/+(.*\.xml)$  /script.php/$1  [L]

Last edited by Nominal Animal; 07-05-2011 at 10:11 PM.
 
Old 07-06-2011, 11:13 AM   #4
PlymWS
Member
 
Registered: Aug 2006
Posts: 51

Original Poster
Rep: Reputation: 16
Thanks to both for your replies. I have tried both and they both 404 on sub directories. Looking at the web console in FF it seems that a subdirectory xml isn't rewritten.

The requested URL /sub1/result.xml was not found on this server.

when trying to rewrite http://sub.domain.com/dir/result.xml to http://sub.domain.com/script.php/dir/result.xml
 
Old 07-06-2011, 12:02 PM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,167
Blog Entries: 1

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Quote:
Originally Posted by PlymWS View Post
Thanks to both for your replies. I have tried both and they both 404 on sub directories. Looking at the web console in FF it seems that a subdirectory xml isn't rewritten.

The requested URL /sub1/result.xml was not found on this server.

when trying to rewrite http://sub.domain.com/dir/result.xml to http://sub.domain.com/script.php/dir/result.xml
You didn't say anything about subdirs in your 1st. Anyway, this should work:
Code:
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^(.*)\.xml$ script.php/$1.xml
Regards
 
Old 07-06-2011, 12:20 PM   #6
PlymWS
Member
 
Registered: Aug 2006
Posts: 51

Original Poster
Rep: Reputation: 16
Yeah, sorry subdirectories was an after thought. Anyway all working. Many thanks.
 
Old 12-17-2011, 03:03 PM   #7
agentgates
LQ Newbie
 
Registered: Dec 2005
Location: Lincolnshire, UK
Distribution: Debian
Posts: 11

Rep: Reputation: 0
Hi All,

I got a multi-site/multi-vhost webserver with apache2 + drupal7 and squeeze. Everything is running fine but I want to redirect all root domain requests to the www vhost from the apache config files instead of .htaccess. That is where I got stuck as all the examples I found were doing it in the .htaccess and they use RewriteCond.

Since the requested URL is already known in the corresponding vhost's section further search would be unnecessary therefore I've commented out the condition. It doesn't work either way.

Do I need to trigger a true condition before going to the RewriteRule or is that true by default? If so how to do that?

This is how I tried:
Code:
<VirtualHost example.com:80>
    RewriteEngine on
#    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R,L]
</VirtualHost>
Many thanks for any suggestion

Regards
Tony

Last edited by agentgates; 12-17-2011 at 03:08 PM. Reason: typos
 
Old 12-17-2011, 04:40 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,167
Blog Entries: 1

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Hi,

Quote:
Do I need to trigger a true condition before going to the RewriteRule or is that true by default? If so how to do that?
You need the condition, because otherwise you'll get a loop, as apache will run the RewriteRule regardless of the HTTP_HOST variable (i.e. for both example.com and www.example.com)


BTW, next time please don't hijack others threads. Start you own thread, so it gets the attention it deserves

Regards
 
Old 12-17-2011, 05:19 PM   #9
agentgates
LQ Newbie
 
Registered: Dec 2005
Location: Lincolnshire, UK
Distribution: Debian
Posts: 11

Rep: Reputation: 0
Hi Bathory, thanks for the response.

Quote:
Originally Posted by bathory View Post
You need the condition, because otherwise you'll get a loop, as apache will run the RewriteRule regardless of the HTTP_HOST variable (i.e. for both example.com and www.example.com)
No, apache doesn't run the RewriteRule at all and there is no loop. Also the www.example.com has a separate VirtualHost section that points to the appropriate html dir. So if the RewriteRule would work apache would go to the corresponding VirtualHost section leaving this.

The rewrite module is installed and works perfectly with all the drupal sites, they all use clean URLs.

Quote:
Originally Posted by bathory View Post
BTW, next time please don't hijack others threads. Start you own thread, so it gets the attention it deserves
That was the original scenario but when I attempted to submit my topic the CMS suggested me to search for existing threads before starting a new one. It also gave me related search results by default so I picked this topic from the list.
 
Old 12-17-2011, 05:42 PM   #10
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,167
Blog Entries: 1

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Quote:
That was the original scenario but when I attempted to submit my topic the CMS suggested me to search for existing threads before starting a new one. It also gave me related search results by default so I picked this topic from the list.
I guess it suggested you to look for answers in similar threads, but you could still start your own.
So please do so, posting distro, apache config files (especially the vhosts), so we can try to find what's wrong.

Regards
 
  


Reply



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
Need help with simple mod_rewrite usage (rewriting all subdomains). win32sux Linux - Server 1 09-07-2008 05:42 PM
URL Rewriting mrpraveen.p Linux - Newbie 1 06-03-2008 07:07 AM
URL Rewriting getmeontop Linux - Newbie 1 04-02-2008 02:12 AM
mod_rewrite for rewriting proxied path GSMD Linux - Server 0 02-28-2007 08:01 AM
mod_rewrite not rewriting urls chr15t0 Linux - General 1 11-09-2003 04:45 AM

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

All times are GMT -5. The time now is 01:04 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