Redirect 301 Apache to new site - Folders - php

I wan't to redirect
www.site123.com/images/folder01/img01.jpg
to
www.site456.com/images/folder02/img01.jpg
I want to redirect the folder to folder separately. One line for each folder.
Entering in:
www.site123.com/images/folder01/sub01/sub02/999.xyz
Should be redirected (301) to:
www.site456.com/images/folder01/sub01/sub02/999.xyz

Since the directory structure from site123 to site456 look identical, have you tried the following global redirect?
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.site456.com/$1 [R=301,L]
If directory/specific then change your rule to something like this:
RewriteRule ^/folder/(.*) http://www.site456.com/folder/$1 [R=301,L]
If you want sub-folders too:
RewriteRule ^Images/?(.*) http://www.site456.com/Images/$1 [R=301,L]
Create a line for each pattern you wish to match and change the corresponding redirect URL/folder path. With rewrites it's sometimes trial/error too so take this and try it, then tweak as needed.

Related

Permanent redirect htaccess to a new domaine keeping a part of the URL as it is

I never wrote any htaccess codnitions and the one by default dont give me enough to work with to get what I need,
I have a URL like : http://domaineA.com/target:/to/be/removed/keep/this.ext
And I want to redirect all URLs like this one to :
http://domaineB.com/keep/this.ext
I tried this, but didn't work:
//301 Redirect Entire Directory
RedirectMatch 301 http://domaineA.com/target:/to/be/removed(.*) http://domaineB.com/$1
Update:
I forgot to mention that the /keep/this.ext is dynamic, it represents all files of a my directory.
Thank you
You can use this rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domaineA\.com$ [NC]
RewriteRule ^/?target:/to/be/removed(/.*)?$ http://domaineB.com$1 [L,NE,R=301,NC]
you can put this in your .htacces or your virtualhost as below
Redirect permanent http://domaineA.com/target:/to/be/removed/keep/this.ext http://domaineB.com/keep/this.ext

Mod_Rewrite: Redirect all reqs to subdirectory so subsequent redirects don't require the subdirectory

I'm redirecting my root to a subdirectory on my server:
RewriteEngine On
RewriteRule ^(.*)$ /_from_git/$1 [L]
In that subdirectory (_from_git/) will also be .htaccess files with redirects. I want my like below redirects to be exact matches using the start (^) and end ($) selectors, but I don't want to have to include ^_from_git/ in every rewrite.
So my question is - How can I modify my root redirect above, so my subsequent redirects can be
like this:
^old_page\.html$ /new_page.html [R=301,L]
and not like this:
^_from_git/old_page\.html$ /new_page.html [R=301,L]
Thanks!
edit: Here you can see the location of my .htaccess files:
Root .htaccess:
Subdirectory's .htaccess:
If you create a /_from_git/.htaccess file then you can have these rules without the need to prefix each rule pattern with /_from_git/:
RewriteEngine On
RewriteRule ^old_page\.html$ /new_page.html [R=301,L,NC]

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Is there a way to get this by .htaccess file?

In my site root directory. there are donghua.php and index.php. now, is there a way to use .htaccess file to let the visitor access my site .the default shows example.com/donghua.php. not example.com/index.php. thank you. the server is Apache.
ps:The user still can access example.com/index.php
i using DirectoryIndex donghua.php in the .htaccess. the default page is ok. but when i access example.com/index.php it redirect to example.com/donghua.php. if i forbid it redirect how do i do?
If you have access to the virtual host config file (or you should even be able to do this in htaccess) you can set the DirectoryIndex to be donghua.php if you want, i.e. instead of something like this:
DirectoryIndex index.html index.php default.html
Just do:
DirectoryIndex donghua.php
So whenever someone goes to your website, example.com, your apache installation will read donghua.php as the default index file.
Change your redirection rule in .htaccess file for index.php. If you open the .htaccess file, you can find the follwing lines,
RewriteCond %{THE_REQUEST} ^.*/example.php
RewriteRule ^(.*)example.php$ http://www.test.com/$1 [R=301,L]
Change or remove this rule for index.php. Better remove or comment it then it won't redirect to another page
"RewriteCond" this code check the codition if the browser requesting the file name index.php. If the condition is satisfy, then the next line will execute that is "RewriteRule". This "RewriteRule" will redirect your URL.
Search and find the name "index.php" in .htaccess file. comment out that lines. syntax to comment is "#".
Example:
#RewriteCond
#RewriteRule
Take back up before changing anything in .htaccess file.
To make your URL lovely and suitable for search engine ranking.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]
To redirect
redirect 301 /old-page.php http://www.yoursite.com/new-page.php

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories