Open file one directory below - url-rewriting

I can't really explain or title this one so I'll use an example.
On my website I want /img/image.jpg/1.jpg to open image.jpg when opened but keep that URL. Basically I want that whatever is entered after image.jpg/ doens't matter and it would always open image.jpg. Could it be done with .htaccess setting or would I need to do something more advanced?

Yes, this can be done using .htaccess :
RewriteEngine On
RewriteRule ^img/image.jpg/.*$ image.jpg
EDIT :
To make image.jpg a variable too, you have to use brackets like this :
# This rule will match every URL img/nameofimage.jpg/randomcaracters and will redirect to nameofimage.jpg
RewriteRule ^img/([a-zA-Z0-9]+\.jpg)/.*$ $1

Related

mod_rewrite rule for file to be downloaded from another directory

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]

.htaccess will not redirect .php files

I have this in my htaccess, standard domain redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/fullnewdomain\.org\/$1" [QSA,R=301,L]
And it works fine for folders, subfolders, html files, images, etc. However, for some reason it refuses to redirect php files. Instead they still run as normal and do not redirect to the new domain. Any ideas as to why, and how I can fix it? It's almost like this host is trying to execute the php file before checking any rules ( And I'm not sure what I could do if that's the case! ).
Turns out there was an htaccess file the host had placed in the home directory ( One level above public_html ) to "counter" bots, but all it did was break anything else that tried to apply a rule to php files. Removed the file, problem solved itself.
For reference, hostgator was the host, and I still don't know why they felt the need to place the file there in the first place.

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).

Resources