RobFos
RobFos

Reputation: 961

.htaccess mod_rewrite redirect issue

I have currently hit a bit of an issue with redirecting users with .htaccess and was wondering if anyone could help me.

1. Background:

I currently have a rather long domain name for the sake of this question lets refer to it as mylongdomainname.com now on this domain I have a subdomain that I use to host files, pictures etc to show friends or share with people this is files.mylongdomainname.com now obviously the URL can get quite long as I have different directories and files. so to help reduce a bit of space I purchased another short domain, lets refer to this as small.me now what I want to do is use .htaccess and a simple PHP file to redirect small.me to files.mylongdomainname.com and pass on a file reference.

Example: small.me/pictures/example.jpg should redirect to files.mylongdomainname.com/pictures/example.jpg

2. The problem

Basically I am unsure on the exact rewrite rule I would need to acomplish this. obviously I need a rule that will allow anything after small.me/ to be sent with the GET method to the index file which would then redirect the user accordingly. So that means that the rewrite rule will have to allow all letters, numbers and valid file name symbols to be used. I'm sure it's simple but after looking at a few tutorials and mod_rewrite help sites I still have no idea how to accomplish this.

3. The Code

.htaccess

RewriteEngine on
RewriteRule ^$ index.php?file_location=$1 [L]

obviously wrong

index.php

<?php

    //Get the requested files location.
    $file_location = $_GET['file_location'];


    //Redirect the user to the file.
    header('refresh:2; url=http://files.mylongdomainname.com/' . $file_location);

?>

4. Notes

I am aware I could just use a URL shortener, but because I am awkward I would rather it just went through my own domain, so please don't comment or answer telling me to use a shortener or to use a service like dropbox.


So can anybody help me by providing the right rule? Any help is much appreciated.

Upvotes: 3

Views: 210

Answers (3)

syserr0r
syserr0r

Reputation: 654

The following in your .htaccess file should be all you need (no PHP file needed):

RewriteRule ^(.*) http://files.mylongdomainname.com/$1 [RL]

For more information and examples see the mod_rewrite documentation

Upvotes: 1

Abhinav Pandey
Abhinav Pandey

Reputation: 499

Assuming you want this:

Picture to be shared: http://files.mylongdomainname.com/pictures/me/troll.jpg

Desired URL: http://small.me/pictures/me/troll.jpg

Remove the PHP file, just Place this in small.me's htaccess:

RewriteRule ^\/?(.*)$ http://files.mylongdomainname.com/$1 [NC,L]

Upvotes: 1

Andre
Andre

Reputation: 3181

In .htaccess you can simply use:

RewriteRule ^(.*)$ http://small.me/?$1 [L]

No need for the PHP file if that's all you're trying to do.

Upvotes: 3

Related Questions