LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Rewriting a URL with mod_rewrite (https://www.linuxquestions.org/questions/linux-server-73/rewriting-a-url-with-mod_rewrite-890070/)

PlymWS 07-05-2011 02:36 PM

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 ?

bathory 07-05-2011 04:24 PM

Hi,

Try this:
Code:

Options +FollowSymlinks
RewriteEngine on

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

Regards

Nominal Animal 07-05-2011 10:10 PM

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]


PlymWS 07-06-2011 11:13 AM

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

bathory 07-06-2011 12:02 PM

Quote:

Originally Posted by PlymWS (Post 4406950)
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

PlymWS 07-06-2011 12:20 PM

Yeah, sorry subdirectories was an after thought. Anyway all working. Many thanks.

agentgates 12-17-2011 03:03 PM

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

bathory 12-17-2011 04:40 PM

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

agentgates 12-17-2011 05:19 PM

Hi Bathory, thanks for the response.

Quote:

Originally Posted by bathory (Post 4552614)
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 (Post 4552614)
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.

bathory 12-17-2011 05:42 PM

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


All times are GMT -5. The time now is 02:37 PM.