mod_rewrite help - mod-rewrite

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

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 to parent directory

I would like to create a .htaccess file that would do this logic:
If the requested file is not found try to find it in the directory
above.
I don't want to redirect the browser I would just like to internally rewrite the request.
I tried and searched for this a lot but always got stuck because (as I gather from the log) the where I could do the rewrite the path was always already without its per directory prefix. In the example below the .htaccess file is in the lang folder. If the lang specific file is not found it should just take the file from the parent folder. I understand that it is possible to do it by hardcoding the parent directory or by placing the .htaccess higher, but now that I suffered for so long in trying I would be very interested to learn if it was possible at all this way.
strip per-dir prefix: X:/localhost/htdocs/peopletest/public/img/root_cli/lang/en/loginhead.gif -> en/loginhead.gif
applying pattern 'somePattern' to uri 'en/loginhead.gif'
Thanks for the help.
SWK
Like this?
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-l
RewriteRule /[^/+]/([^/]+)$ $1

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]

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