mod_rewrite rule for file to be downloaded from another directory - mod-rewrite

Note: I couldn't find any similar question. Please let me know if duplicate question exists. I don't want to put duplicate question intentionally.
I have directory structure longer which I don't want users to find out as well as I want download file URL to be fancy.
My directory structure is
Actual File Path = ./my_dir/files/upload/user_images/user_file.pdf
My Domain is having ./my_die/ as Document Root so example.com will serve contents of ./my_dir/
To download file I have http://example.com/files/upload/user_files/user_file.pdf
Which I want to convert to
http://example.com/user/uploads/user_file.pdf
So I need MOD Rewrite Rule to convert as following:
http://example.com/files/upload/user_files/user_file.pdf to http://example.com/user/uploads/user_file.pdf

You may use this rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^user/uploads/(.+)$ files/upload/user_files/$1 [L,NC]

Related

Is it possible to get `www.mydomain.com/app` to point to `www.myapp.heroku.com`?

I have www.myapp.heroku.com and domain name www.mydomain.com.
Is it possible to have www.mydomain.com/app point to www.myapp.heroku.com?
Googling seems to only yield how to get www.mydomain.com to point to www.myapp.heroku.com. I can't find anything about forward slashes.
Try it with HTACCESS (If unknown create a blank file in your base directory and rename it to: .htaccess
Now add:
RewriteEngine on
Redirect /app http://www.app.yourdomain.com
Just tested it, will work (if your provider supports htaccess url forwarding)
Good Luck

mod_rewrite from a subfolder with a querystring to a folder or file

I am trying to redirect from:
http://www.example.com/folder/product.aspx?prodid=146
to
http://www.example.com/folder2/folder3/
The folders referred to here don't really exist. There are other rewrite rules in place which redirect transparently to the actual content.
If I create a directory called 'folder', and put an .htaccess file in it, I can get the redirect working, BUT, other URLs which refer to that folder no longer work. So I have to try and do the redirect from the .htaccess file in the ROOT folder.
I tried this:
RewriteCond %{QUERY_STRING} prodid=146
RewriteRule ^/folder/product.aspx$ /folder2/folder3/? [R]
...but it doesn't work (I get a 404 error). Using identical syntax but omitting the /folder/ from the 2nd line works if the .htaccess is in the folder directory (so I know the above can't be too far off) - but as I said, I cannot do that. I have tried lots of variations but nothing seems to work. Any assistance appreciated.
You need to remove the slash from the start your URL regexp. Like this:
RewriteRule ^folder/product.aspx$ /folder2/folder3/? [R]

mod_rewrite help

I want to hide my directory structure and have all requests served out of a single directory where my files are located. The actual path where my files are stored is: http://mydomain.com/dir1/dir2 but I'd like to be able to just point my links to just http://mydomain.com/myscript.php.
I have multiple script in this directory so I'm not sure how to go about this. Would I need a rule for each and every file I need access to or is there a wildcard that I can use for this?
You can do this:
RewriteCond %{DOCUMENT_ROOT}/dir1/dir2%{REQUEST_URI} -f
RewriteRule !^/dir1/dir2/ /dir1/dir2%{REQUEST_URI} [L]
This will rewrite any request, that’s path does not begin with /dir1/dir2/, to the corresponding location with that prefix /dir1/dir2/ but only if there is a file at that destination (see RewriteCond directive).

Rewriting URLs and "faking" folders

I'm trying use mod_rewrite to rewrite URLs from the following:
http://www.site.com/one-two-file.php
to
http://www.site.com/one/two/file.php
The folders don't exist, but "virtually" exist for the rewriting purpose.
What rule do I used in this?
Untested:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.php$ $1-$2-$3.php [L]
I can't really understand your explanations about virtuality and existence: one-two-file.php must exist or you'll have nowhere to redirect to.
Update
The previous version works fine when used from an .htaccess file. However, if used from main http.conf file you need to add leading slashes:
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)\.php$ /$1-$2-$3.php [L]
I presume that's why it wasn't working for the OP (he was probably getting a 404 not found status code).

How can I get mod_rewrite to ignore certain directories and all their subdirectories?

I'm using Symphony CMS which as default has a mod_rewrite that rewrites all directories.
However, I need it to ignore the directories test and transfer and all their subdirectories.
Place a new .htacess access files in each of your two directories, and then place the following in:
mod_rewrite off
That should sort it.
Put this rule above the others:
RewriteRule ^(test|transfer)(/|$) - [L]

Resources