One more Redirect 301 - .htaccess

I should do internal redirects (301) for the list of pages.
Firstly when user opens / or /index.php he should be redirected to em.php, places in root folder. When user opens /contents/about_us/index.php he should be redirected to about_us/enterprise.php
Sound simple but I still can't to solve
When I use this
Redirect 301 / http://foo2.bar.com/service
Redirect 301 /index.php http://foo2.bar.com/service
it works.
But when I try this
Redirect 301 / http://www.site.com/em.php
I'm getting http://www.site.com/em.phpem.phpem.phpem.phpem.phpem.phpem.phpem.phpem.php ...
What's wrong with my code?

Your Redirect is going into a loop.
Do this instead either in a .htaccess file in DocumentRootor in your virtualhost section.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^ /em.php [L,R=301]

Related

.htaccess redirect only root of a subfolder to another subfolder?

I have a site in "example.com/a" and would like to redirect 301 just url "example.com/a" to "example.com/b" and other data in "example.com/a" dont change address.
create .htaccess in "example.com/a" and try using several code like:
RewriteEngine on
RewriteRule ^$ http://www.example.com/b [R=301,L]
and
RedirectMatch 301 ^/$ https://example.com/b
and its redirect root but all other data in "example.com/a" show 404 error.
I have a lot of photos in "root/a" , just want to redirect the "root/a" address to "root/b" and the address of the photos remains the same.
For example, the "root/ a/pic.jpg" address will remain unchanged before and after the redirect,
How can i just redirect the "example.com/a" and other old address not change?
If I understood you well you need to redirect root/a to root/b while maintaining location of the rest of the pages, files?
This will do:
# Redirect Root Only
<IfModule mod_rewrite.c>
RewriteEngine On
#Uncheck line below to redirect 'index.php' as well
#Redirect 301 /a/index.php http://example.com/b/
#Redirect root only
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/a/$
Rewriterule ^(.*)$ http://example.com/b/ [L,R=301]
</IfModule>
# END Redirect Root Only
Need to redirect index.php, index.html, or other index pages too? Uncheck the line in code above. Because %{REQUEST_URI} ^/a/$ excludes all direct file paths including /a/index.html,index.php,index.py,etc.

301 /product?title=jar&id=190 to homepage

i'm trying to redirect a page to homepage and cant seem to make it work with .htaccess for some reason
/product?title=jar&id=190 to homepage
i've already tried
Redirect 301 ^/product?title=jar&id=190 /
and
Redirect 301 ^/product?title=jar&id=190 /index
and a couple other redirects but for some reason they are not working
Redirect directive works on URL path only. Since your URL contains a query String title=jar&id=190 you will need to use RewriteRule for this .
RewriteEngine on
RewriteCond %{QUERY_STRING} ^title=jar&id=190$ [NC]
RewriteRule ^product/?$ /? [L,R=301]

Redirect 301 .htaccess Redirect Error

I have re-vamped a website and I'm attempting to ensure all of the old, deleted directories are redirected to the new pages. For example, I have a directory called /category/ and want it redirected to http://website.com/interiordesign.html. The root website is still the exact same. I've implemented some coding in the .htaccess file, but it is giving me a redirect error.
Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
Redirect 301 /category/ http://website.com/interiordesign.html
Redirect 301 /interior-design-portfolio/ http://website.com/interiordesign.html
Redirect 301 /contact-location/ http://website.com/contact.html
Redirect 301 /tag/ http://website.com/blog.html
I have the remove / rule in place, as I found when I didn't implement that, it was taking the user to interiordesign.html/ and with the slash, it didn't work.
Apologies for my poor coding, this is not my area of expertise.

Redirect entire site to a sub folder

I am trying to redirect my entire site to a subfolder present in the root directory using the following Redirect command.
Redirect 301 / http://example.com/folder/
However, when I open the website, it gets redirected to something like
http://www.example.com/folder/folder/folder/folder/folder/folder/folder...
Am I doing something wrong here?
Yes, you're redirecting anything starting with / so of course that includes /folder/ and it just keeps redirecting. You can't redirect your whole site to a part of itself without excluding that part.
Use this instead:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [NE,R=301,L]
It will redirect anything that is not in /folder/.
To only redirect the homepage, use this instead:
RewriteEngine on
RewriteRule ^$ /folder/ [R=301,L]

URL-based redirect with Htaccess

I have several domain aliases pointing to the same website like:
domain1.de/en/
domain1.de/de/
domain2.ch/de/
domain2.ch/en/
When someone opens a specific language-based sub-directory on a URL, I would like to redirect them to the start page, e.g. for domain1.de users shouldn't be able to access the sub-directory /en/ but be redirected to the start page. For the domain2.ch users should only have access to the directory /en/ and not /de/. How can I set this up with Htaccess?
This might be a helpful link to you;
htaccess redirect
for a 301 redirect entire directory it generated the following code:
//301 Redirect Entire Directory
RedirectMatch 301 domain2.ch/de/(.*) domain2.ch/en//$1
RedirectMatch 301 domain1.de/de/(.*) domain1.de/en//$1
Give this a try:
RewriteCond %{HTTP_HOST} ^de\.domain\.de$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)/en/(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/targetpage.php [R=301,L]

Resources