redirect whole directory to another - .htaccess

I need to redirect whole directory to another
like :
http://test.com/en/file1.php
http://test.com/en/file2.php
http://test.com/en/file3.php
http://test.com/en/file4.php
http://test.com/en/directory
to
http://test.com/fa/file1.php
http://test.com/fa/file2.php
http://test.com/fa/file3.php
http://test.com/fa/file4.php
http://test.com/fa/directory
how ?

You can place this line as first line in your root .htaccess:
RedirectMatch 301 ^/en/(.*)$ /fr/$1

Related

Redirect a non existent directory to a normal directory using .htaccess on Joomla

I would like to ask how is possible to redirect an non existent directory to a normal directory. Or even better, how is it possible to remove a directory from the url.
Example:
When the url is www.mysite.com/bad_directory/images/and/sub/directories I want it to be www.mysite.com/images/and/sub/directories. In other words I want the directory "bad_directory/" to be removed when directory "images" is requested.
You should add this line in your .htaccess file :
RewriteRule ^bad_directory/images/and/sub/directories$ /images/and/sub/directories [R,L]
Well I found the solution
At .htcaccess file, I added the line:
RedirectMatch ^/bad/directory/(.*)$ https://www.example.com/good-directory/$1

How to redirect /page/index/About-Us/ to /About-Us

I would like to redirect
/page/index/About-Us/
link created in codeigniter framework to
/About-Us
where the word 'About-Us' is dynamically created and can be any word may be 'contact-us' the next time.
How can I do it with .htaccess?
You can use the following in htaccess in the root :
RedirectMatch ^/page/index/(.+)$ /$1
Try with routes. At the end of APPPATH . 'config/routes.php' file write:
$route['(:any)'] = 'page/index/$1';

How to redirect to subdirectory using .htaccess?

I do not understand htaccess fully so I need help solving this. I need to redirect users from the directory www.example.com/es and www.example.com/en to the subdirectory www.example.com/old/es and www.example.com/old/en.
Add this in the htaccess file in your document root:
RedirectMatch 301 ^/(en|es)(/.*)?$ /old/$1$2
If you only want to redirect /en and /es, and not /en/foo or /es/something/else then remove the $2 bit from the end.

.htaccess 301 redirect based on second to last and last directory

I'm better with front-end development so I would really appreciate help with an .htaccess 301 redirect.
I want to 301 redirect /anything-here/rc/code/ to /anything-here/?rc=code
Example 1: //www.domain.com/any_directory/other_directory/rc/code/ should 301 redirect to //www.domain.com/any_directory/other_directory/?rc=code
Example 2: //www.domain.com/rc/code/ should 301 redirect to //www.domain.com/?rc=code
It should also retain a potential query string (any number of variables).
Example 3: //www.domain.com/any_directory/other_directory/rc/code/?variable1=value1&variable2=value2 should 301 redirect to //www.domain.com/any_directory/other_directory/?rc=code&variable1=value1&variable2=value2
It should not redirect if the second to last director is not 'rc' (exact match).
Example 4: //www.domain.com/any_directory/rc/other_directory/code/ should NOT redirect ('rc' is not second to last directory)
Example 5: //www.domain.com/code/rc/ should NOT redirect ('rc' is not second to last directory)
Example 6: //www.domain.com/arc/code/ should NOT redirect (no 'rc' directory)
Example 7: //www.domain.com/rc/ should NOT redirect ('rc' is not second to last directory)
I have another line that adds a trailing slash if it's missing so it's not relevant in this case. Many thanks in advance!
You may try this:
RewriteEngine On
RewriteBase /Test
RewriteCond %{REQUEST_URI} ^(.*)/rc/(\w+)/?$
RewriteRule ^ %1/?rc=%2&%{QUERY_STRING} [R=301,L]
When not redirecting as in: http://www.domain.com/code/rc/ the folder rc MUST EXIST otherwise an error 404 is generated.
You should not add a trailing slash to the query.
http://www.domain.com/any_directory/other_directory/?rc=code&variable1=value1
is different from:
http://www.domain.com/any_directory/other_directory/?rc=code&variable1=value1/
The last variable1=value1/ will have a wrong value.

.htaccess redirect path with or without trailing slash

Given the following path:
http://www.testsite.com/some/path/
I want to redirect it to
http://www.testing.com/new/stuff/
How can I make .htaccess execute that redirect regardless of whether the path has a trailing slash or not?
Edit
I've tried this:
RedirectMatch 301 /some/path(.*) /new/stuff/$1
However, it results in the following redirect
http://www.testing.com/new/stuff/?/some/path
how can I make redirect from one path to another without appending anything to the new path. I just want it to go to '/new/stuff/' with nothing on the end regardless of what, if anything, comes after '/some/path'
Just remove the $1 from your RedirectMatch's target. You should also include the ^ for the beginning of the URI:
RedirectMatch 301 ^/some/path(.*) /new/stuff/

Resources