Question about .htaccess 301 routing An exception 301 routing is in the .htaccess file - http-status-code-301

I have the following command:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
</IfModule>
The command redirects to the same address and removes the slash.
For example:
http://example.com/folder/
Will be redirected to:
http://example.com/folder
I want to add an exception where routing will not work. If the user specifically enters the following address:
https://example.com/a/b/
Then there will be no routing - but only at that specific address.
Does anyone have an idea? Thank you

Check this /a/b/ except in the rule
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/a/b/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
</IfModule>

Related

htaccess redirect retains part of old url

Updating a site and have a ton of redirects.
The issue is that the new URL retains part of the old for all, as seen in the following example:
Redirect 301 /old/oldfile.html /new
redirects to:
http://url.com/new/oldfile.html
Aside from the redirects the htaccess contains the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Use mod_rewrite instead. This should go above the other rewrite rules.
RewriteEngine on
RewriteRule ^old/oldfile.html /new [R=301,L]

.htaccess redirect not properly

I would like to redirect my pages in the following ways:
www.example.com/index.cfm?locale=it -> www.example.com/it
and I would like to access the same page when I type:
www.example.com/it
I wrote an htaccess file but it causes a redirect loop. Here is the file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#REWRITE RULES
#---------------------
RewriteCond %{QUERY_STRING} ^locale=(it|en|fr|de|es)$
RewriteRule ^index\.cfm$ %1? [R=302,L]
RewriteCond %{REQUEST_URI} !^index\.cfm
RewriteRule ^(it|en|fr|de|es)/?$ index.cfm?locale=$1 [L]
</IfModule>
Could you help me to resolve it?
Thanks!!!
You can try it this way.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
Rewrite ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /index\.cfm\?locale=(it|en|fr|de|es)
RewriteRule ^ %1? [R=302,L]
RewriteRule ^(it|en|fr|de|es)/?$ index.cfm?locale=$1 [L]
Try this:
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/it[nc]
RewriteRule ^(.*)$ http://www.example.com/it/$1 [r=301,nc]
//301 Redirect Old File
Redirect 301 www.example.com/index.php?locale=it www.example.com/it
//301 Redirect Entire Directory
RedirectMatch 301 www.example.com/index.php?locale=it(.*) www.example.com/it/$1

htaccess Redirect All Traffic to https

I'm a beginner when it comes to doing stuff with htaccess so please be patient with my dumb question. I know that this has been addressed a lot (here, here, and here for example) but it doesn't seem to work for my situation. My browser displays a "redirect loop" error with this code in my htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Trying to redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
I know that this probably has something to do with the RewriteCond and the RewriteRule before I redirect to https, but I don't really know what I'm doing here and I don't know what to change.
Update:
Some more info that might be helpful:
When I remove the "redirect to https" code and manually type https://my.site.com it loads just fine.
Also, the redirect-to-https thing worked great before I accidentally deleted the .htaccess file in my directory.
The app that I'm trying to redirect resides in a subfolder of another app which also has an .htaccess file. Here's the code for that app:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
</IfModule>
This is what firebug says:
Change order of your rules and keep 301 rules before other internal rewrite rule:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Trying to redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

Redirecting HTTPS to HTTP via .htaccess

I'm using a WordPress with its default .htaccess and I need to redirect all urls starting with https:// to http://. My server is using the same folder for http and https protocols...
this is my current .htaccess:
# BEGIN WordPress <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L] </IfModule>
# END WordPress
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
It works fine for https://www.domain.com/ (redirects to http://www.domain.com/) but it doesn't work with https://www.domain.com/subpages/ (it also redirects to http://www.domain.com/).
Do you see any problem in .htaccess please?
Thank you
Change the order of the rules:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
Otherwise the WordPress rule will catch all request that’s URL path is not empty (i.e. not just /).

Resources