Redirect too many in htaccess - .htaccess

Currently I'm trying to work on my .htaccess file
What I want to do is to redirect all request to https://example.com/
Means all request for different pages of the domain should be redirect to the homepage/landing page for now.
I tried to do this in .htaccess
Redirect 301 / https://example.com/
But this gives me an output like this
redirect too many times

Well, looks like your are also redirecting requests to https://example.com/ to https://example.com/, right? ;-)
You need to take care that you only apply your redirection rule if the incoming request does not already request exactly that URL you are trying to redirect to. Two approaches here:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ https://example.com/ [R=301,L]
Or just:
RewriteEngine on
RewriteRule ^/?.+ https://example.com/ [R=301,L]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

redirect pdf files .htaccess

I am trying to redirect an url like that: website_name/download?q=filename.pdf to website_name/resources/filename.pdf
Also, website_name/download?q=filename.pdf is not existing but the users has a chance to find this link so that is why I want to redirect it.
I wrote the following code in my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /download?q=$1
RewriteRule ([^\/]+\.pdf)$ /resources [NC,L,R=301]
</IfModule>
but for some reason it is not working. If anyone has an idea! Thank you
Your question is a bit vague, but I understand that you want something like this combination of an external redirection and an internal rewrite:
RewriteEngine On
# external redirect from /download?q=file.pdf to /resources/file.pdf
RewriteCond %{QUERY_STRING} (?:^|&)q=([^&]+\.pdf)(?:&|$)
RequestRule ^/?download /resources/%1.pdf [R=301]
# internal rewrite from /resources/file.pdf to /download?q=file.pdf
RewriteRule ^/?resources/([^/]+\.pdf)$ /download?q=$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Simple .htaccess redirect within the same domain

A bit of a newbie question, how can I redirect a URL like this one: http://example.com.ar to http://example.com.ar/pictures/ using .htaccess ?
or would it be easier to make a rule that searches the entire request URL and adds a /pictures/ when not found?
I have tried:
RewriteBase /
RewriteCond %{HTTP_HOST} example.com.ar [NC]
RewriteRule ^(.*)$ http://www.example.com.ar/pictures/ [R=301,NC]
But it didn't work :/
Sounds pretty straight forward. You would have found hundreds of existing examples...
RewriteEngine on
RewriteRule ^/?$ /pictures/ [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

how to htaccess redirect from index.php to / only in specific folder?

I want to create a redirect in my htaccess file. From URL with index.php to page without index.php, but only for one specific folder "buy-new-cars".
For example:
from example.com/buy-new-cars/index.php to example.com/buy-new-cars/
I try to add those lines.But this didn't work.
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(buy-new-cars/.+)index\.php$ /$1 [L,R=302,NC,NE]
Your issue is the RewriteCond you use which does not make sense.
Here is a version with some additional modifications:
RewriteEngine on
RewriteRule ^/?buy-new-cars/(.+)/index\.php$ /buy-new-cars/$1 [R=302,NC]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to fix 301 Redirect for this URL

I'm trying to transfer over
www.old.example/ecards/birthday-ecards/all
which no longer exists to
https://www.new.example/static-ecards/company-birthday-ecards.html
When using .htaccess to complete this I'm getting it redirecting to
https://www.new.example/static-ecards/company-birthday-ecards.htmlecards/birthday-ecards/all
So it's adding this to the end of the URL ecards/birthday-ecards/all which is not needed.
I'm a noob with this and not a dev! Please advise.
I have tried
Redirect 301 "/old-folder/" "http://www.new.example/new-page.html"
Redirect 301 "/old-page.html" "http://www.new.example/new-page.html"
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old\.example$
RewriteRule ^/?ecards/birthday-ecards/all$ https://www.new.example/static-ecards/company-birthday-ecards.html [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Add this rule on top of old.example .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^ecards/birthday-ecards/all$ https://www.new.example/static-ecards/company-birthday-ecards.html [R=301,L]

Redirect addon domain to webapge htaccess

Not quite sure if this is possible. So I have domain2.com and it maps to domain.com.
On the site I have a page called /domain-2-landing
When I hit domain2.com I want that to redirect to domain2.com/domain-2-landing or even better yet mask domain.com and serve the content of /domain-2-landing
Is this possible via .htaccess?
Your question is a bit vague, since you write about two separate things. Here are two approaches that hopefully will point you into the right direction:
To redirect any request to "domain2.com" to that "landing page" this probably is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2\.com$
RewriteCond %{REQUEST_URI} !^/domain-2-landing$
RewriteRule ^ /domain-2-landing [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
To deliver the content of that page as a response to requests to "domain.com" there are two diffferent situations:
If the domains are served from separate http servers you can use the proxy feature integrated into the rewriting module if the proxy module is installed:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ https://domain2.com/domain-2-landing [P,END]
If both domains are served from a single http server you can do something similar as above, if both hosts share the same DOCUMENT_ROOT:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ /domain-2-landing [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources