Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule

URL rewriting

         

xt35

9:47 pm on Dec 27, 2005 (gmt 0)

10+ Year Member



Hi,

How can I rewrite the URLs for more files? By example, I want to move my image files (.jpg) from the root directory in a directory named "pics".

I know how do do this for the files one by one, by example:

RewriteRule ^MyPicture\.jpg$ pics/MyPicture.jpg [R]

but how can this be done for all the .jpg files from the root?

Something like

# RewriteRule ^(.*)\.jpg$ pics/? [R]

Thank you.
xt

jdMorgan

11:23 pm on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



xt,

Welcome to WebmasterWorld!

To *rewrite* only root-directory .jpg files to /pics subdirectory:


RewriteRule ^([^/.]+)\.jpg$ /pics/$1 [L]

To *redirect* only root-directory jpg files to /pics subdirectory:

RewriteRule ^([^/.]+)\.jpg$ http://www.example.com/pics/$1 [R=301,L]

In the first example, the client (browser or robot) will not be aware that the images have been relocated. In the second example, the server sends a 301 redirect to the client, and the client will re-request the image from the subdirectory.

Jim

xt35

11:58 pm on Dec 27, 2005 (gmt 0)

10+ Year Member



Thank you Jim :)

I've tested both your suggestions and they didn't work. Are you sure they are working for you?

The rewriting for 1 file (without wild chars) works though.

Thanks,
xt

KavanR

3:44 am on Jan 5, 2006 (gmt 0)



I wish to virtual host a second domain at my webserver using RewriteRule.

The main aim is to ensure that those pages are indexed in google with the correct domain name.

Main domain is http://example.org

I created a subdirectory http://example.org/net/

Could someone please tell me how I can construct a RewriteRule to put in .htaccess such that

http://example.org/net/$1

Will appear on users browser like

http://example.net/$1

Where $1 is any file, in any subdirectory below
net/ that is referenced

Thanks for any help

Kavan

jdMorgan

5:41 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Kavan,

Welcome to WebmasterWorld!

You can't do what you're asking in the way that you are expecting. The process is backwards.

1) Change all links on all of your pages to point to the URLs you want to show in the browser.
2) Use mod_rewrite to detect those URLs when they are requested from your server, and 'point' them to the correct server directory where the content for those URLs is located.

Before doing all this, you must be sure that you have DNS set up for your .net domain, and that it points to the IP address of your server. You must also be sure that your host has configured the server in httpd.conf to recognize that secondary domain, and point requests to your directory.

If you use a control panel, then do not use mod_rewrite; Use the facilities provided by the control panel itself to do all of this.

If you do end up using mod_rewrite, then the code would be something like:


RewriteCond %{HTTP_HOST} ^(www\.)example\.net
RewriteRule (.*) /net/$1 [L]

I assume that you have other rewrite rules already working, and have all the mod_rewrie setup directives already in place.

Jim