mod_rewrite not excluding folder - apache

I've setup some rewrite rules to cleanup the URLs of my page. These rewrites work fine, except for some downloads where i don't want to rewrite the URL.
I've tried several Rules to exclude the "files" folder:
1#:
RewriteCond %{REQUEST_URI} !/files[0-9]*/
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
2#:
RewriteCond %{REQUEST_URI} !^./files
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
3#:
RewriteRule ^files - [L]
4#:
Putting a new .htaccess file in the "files" folder with
RewriteEngine Off
Are there any other methods I could try?
I do want to redirect to the pdf download i've setup without rewriting. just like:
<a href="files/myDownload.pdf">
If I quote the rewrite rule:
#RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
It no longer rewrites the url and i can access my download, so i just need to get the folder excepted from the rule.
Do you have any Ideas or tips?

All the tries except for #2 should have worked. Please note that the order in which you put your RewriteRules is also very important.
Can you give the following a try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/files/ [NC]
RewriteRule ^ index.php?%{QUERY_STRING} [L]

Related

Remove subfolder of a folder from Url by Htaccess?

I have a URL structure like this -
https://example.com / folder / subfolder / files.html
I have many files in the subfolder. I want to remove the 'subfolder' from the URL.
I have tried some of the StackOverflow solutions. but not succeeded.
Also please tell me where to paste the .htaccess code. in the .htaccess file which is located in 'folder' or in 'subfolder'.
edit
folder/htaccess
RewriteEngine On
RewriteCond {REQUEST_FILENAME} !-f
RewriteCond {REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /folder/subfolder/$1 [L]
subfolder/htaccesss
RewriteEngine On
RewriteRule (.+) /folder/$1 [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.html-f
RewriteRule ^ %{REQUEST_URI}.html[L]
You can use the following rule in your /folder/. htaccess :
RewriteEngine On
RewriteCond {DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond {DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.+)/?$ /folder/subfolder/$1 [L]
If the rule above fails to work, then use the following instead
RewriteEngine On
RewriteCond %{REQUEST_URI} !/folder/subfolder/
RewriteRule ^(.+)/?$ /folder/subfolder/$1 [L]
Remember to place the rule at the top of your htaccess or before other directives and put it in htaccess in your /folder directory.
EDIT :
If you also want to redirect your old URLs to the new one , for example /folder/subfolder/file.html to /folder/file.html , put the following rule in htaccess in folder/subfolder
RewriteEngine On
RewriteRule (.+) /folder/$1 [L,R]
Remember to place the code above in htaccess in your subfolder and when you are sure the rule is working, change R to R=301 to make the redirection permanent as it avoids the duplicate content issues in search engines.
Edit :
If the above solution isn't working for you, as you said in comments you are getting 404 error on redirection. The error is there because the requested file is being rewritten to a non-existentpath/folder/file.html. You can solve this by removing your/folder/subfolder/. htaccessfile and only use one single htaccess in/folder/` with the following contents :
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subfolder/(.+)/?$ /folder/$1 [L,R]
RewriteCond %{REQUEST_URI} !/folder/subfolder/
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|gif|png|jpeg)$
RewriteRule ^(.+)/?$ /folder/subfolder/$1 [L]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

.htaccess Rewrite Within Directory - Hide PHP extension and force trailing slash

I'm trying to hide the .php extension from my files as well as force a trailing slash on the resulting URLs.
Example: A request to /about.php would become /about/ and requests to /about would go to /about/.
The following rewrite code worked perfectly when I was in the root of my hostdomain:
RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://edit.mydomain.org/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://edit.mydomain.org/%1 [R=301,QSA]
However, I need to move my files into a directory of this host name. Adding a directory name to the rules and having the .htaccess in the directory itself didn't work at all and seems to cause a endless redirect.
I looked around StackOverflow and other websites and tried numerous examples and ended up with many different errors with the most common being:
Everything is an endless redirect.
Everything except the directory home page is a 500 Error.
about.php redirects to /about but there's no redirect to /about/ and /about/ displays a 500 Error.
Everything working, but the home page (of the directory) index.php when accessed without a filename goes into an endless redirect.
Things redirect to edit.mydomain.org/home/username/public_html/mydomain.org/edit/pagename.php which obviously doesn't exist.
Thanks for any help! I really need to keep these files in a directory although the .htaccess could go into the host name root if its needed.
The directory for this would be edit.mydomain.org/dave/
Save this as a .htaccess and put it in the 'dave' directory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
This works for me
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Multiple RewriteConds and RewriteRule Stacked Together

I have this apache rewrite rule:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} mycompany.com
RewriteRule ^$ http://mycompany.com/login [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
The only thing I can make sense of is if it's mycompany.com, then the script will redirect to http://mycompany.com/login. If not, then ...
I can't figure out already.
Any idea what does the above script say?
Something quite interesting, not easy to understand.
A google search on the comment texts inside the code gave interesting results: http://www.google.com/search?q=%22%23+we+check+if+the+.html+version+is+here+%28caching%29%22
Edit: if we look at the last lines and knowing that Symfony uses caching (it creates local files with .html extension in the same directories as the URL shows 'em) I can try to explain the lines here
If the requested url is something like http://yoursite.com/blabla/ we try to open an index.html file in that directory. If the file is not there, another cycle of rewriting will happen and the last Cond will be hit (where the file does not exist)
RewriteRule ^$ index.html [QSA]
If something more is in the url, like http://yoursite.com/blabla/blblbl, try to find a file blblbl.html
RewriteRule ^([^.]+)$ $1.html [QSA]
This is the collector of all urls that did not match any of the previous rules or the cached file did not exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

mod_rewrite to a directory

Im trying to create a rewrite rule to a different directory, but unfortunatly its not working.
My current rewrite sends everything back to the index.php file unless the first word in the query string is admin. The rewrite rule for 'admin' is to admin.php, but i actually want it to go to /var/www/html/website.com/admin/admin.php (instead of the file in the same directory).
This is current .htaccess file.
RewriteEngine On
RewriteRule ^(.*\/?).*(css|images|js/)+.*$ - [L]
RewriteRule ^\/?admin\/?(.*)$ admin.php?url=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If i change the rewrite rule for admin to use a different directory..
RewriteRule ^\/?admin\/?(.*)$ /var/www/html/website.com/admin/admin.php?url=$1 [QSA,L]
It breaks the site. Any ideas?
Thanks.
It's actually quite simple, you're using the system path, while web path is needed.
Try the following line:
RewriteRule ^\/?admin\/?(.*)$ /subfolder/admin.php?url=$1 [QSA,L]
Try these rules:
RewriteRule ^admin($|/(.*)) admin/admin.php?url=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]