.htaccess rewrite except images and script files - .htaccess

I have the following rewrite:
RewriteRule ^/blog/(.*)$ http://www.domain.co.uk [NC,L,R=301]
But I only want it to happen if it is not a image file (.jpg, .gif, .png) or a script file (.js). Does anyone know how to set this?
(It is a wordpress blog and I want to redirect all pages but keep the scripts and images active.)
Thanks

RewriteCond %{REQUEST_FILENAME} !^(.+)\.js$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.png$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.jpg$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.jpeg$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.gif$
RewriteRule ^/blog/(.+)$ http://www.domain.co.uk [NC,L,R=301]

I found that I had to edit the .htaccess file located in the wordpress file to this:
RewriteEngine On
RedirectMatch 301 ^/blog/$ http://www.domain.co.uk
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=301,L]
</IfModule>
# END WordPress

Related

Htaccess - routing to a php file

I wish to route all address to my "router.php" file unless it is a file or directory. So far I have this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /router.php [L]
</IfModule>
This works for most urls such as:
foo.dev/contact-us uses the router.php file, and
foo.dev/styles.css uses the actual file as expected
However, when I go to foo.dev it goes to the index.php instead of the router.php page.
Why is this and how do I fix it?
You can use DirectoryIndex directive:
DirectoryIndex router.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . router.php [L]

.htaccess image folder redirect from other server

So I need to do a .htaccess file that allow me to redirect the images link folder (wp-content/upload/) from a different site (http://widesigner.com.br/alessandra/) to be this one (http://www.alessandratonisi.com.br/site/)
Basically it's redirect some image links, example:
http://www.widesigner.com.br/alessandra/wp-content/uploads/2012/03/MG_6058-600x400.jpg
http://www.widesigner.com.br/alessandra/wp-content/uploads/2012/03/MG_9515.jpg
to
http://www.alessandratonisi.com.br/site/wp-content/uploads/2012/03/MG_6058-600x400.jpg
http://www.alessandratonisi.com.br/site/wp-content/uploads/2012/03/MG_9515.jpg
So what you need is redirect a website domain to another one for a specific folder. I don't know if the next line will help you to solve the problem, I don't think that will be the solution since I didn't test it.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/alessandra/(.*)$
RewriteRule ^(.*) https://www.alessandratonisi.com.br/site/%1 [R=302,NC]
Here are a lot of examples that could lead you to the correct answer: https://gist.github.com/ScottPhillips/1721489.
UPDATE:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/alessandra/(.*)$
RewriteRule ^(.*) https://www.alessandratonisi.com.br/site/%1 [R=302,NC]
RewriteBase /site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site/index.php [L]
</IfModule>
# END WordPress

.htaccess - This webpage has a redirect loop

I'm cleaning my URLs and everything looks fine but whenever I try to access any directory such as images etc then chrome shows that "This webpage has a redirect loop." However I want to protect directories inside public_html.
The .htaccess file is inside public_html
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</ifModule>
I want to protect images, css and javascript directories but want to allow access to the admin directory.
Thank in advance.
Change order of your rules and change your trailing slash removing rule:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
# block all directories except admin/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^admin(/|$) - [NC,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>
You can use this remove all code and use this one
option index allow and disallow to be index directory like
This does the trick.
Options All -Indexes
or
IndexIgnore *
for more information please check below link
http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/

.htaccess and subdomains

Hey Folks am seemingly having problems with .htaccess files.
The problem:
I have a wp install in the root directory. In a sub-directory (not a domain) there is Modx. When I enter the url to the root/modx the url is treated like a wp link and it goes back to the homepage. In other words I think the .htaccess is overriding.
The main directory (wp) has this .ht file (after digging around for a solution)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^office/.*$ - [PT]
# BEGIN WordPress
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
This is the one in the subdirectory
RewriteEngine On
RewriteBase /cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
well you could add the following rule to your wp .htaccess just before the RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} !modx

Rewriting only for HTML page requests

I have the following in my htaccess files:
Options +FollowSymLink
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !-D
RewriteRule (.*) index.php [L]
I am guessing the above is the reason why my images are not appearing as I am redirecting everything to index.php?
How can I just limit this to HTML files and PHP files?
Thanks all for any help
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.html$ /index.php [R=301,L]

Resources