htaccess redirection to another domain - php

I am newby in htaccess, please help if you can.
My task is simple, I think.
I have a website, for example newsite.com. It was made on wordpress.
And I have old copy of this site, on other domain, for example oldsite.com.
When I transfered files from oldsite to newsite some of pictures didn't transferred to newsite, but they still exists on oldsite.
Now when pages are loading I checking post thumbnails on newsite, and if they don't exists on newsite, I am replacing newsite url to oldsite url.
But is there any ways to not just replace newsite.com/image.jpg to oldsite.com/image.jpg, but replace newsite.com/image.jpg, to, for example newsite.com/imgs/image.jpg, and in .htaccess file make rule that if url has /imgs/ part, it means that need to look for image in oldsite.com/image.jpg

I think you can do it by below .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
In your new site HTML code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/imgs/$1 [R=301,L,NC]

You can put this configuration in the file of virtualhost.
<VirtualHost *:80>
ServerName example.org
DocumentRoot /srv/redireccion
Redirect permanent / https://example2.org/
</VirtualHost>

Related

Redirect different domains pointing to same server to different version of same website

I have different domains say like (fictious names) "plumbers.org" and "cleaning.org"
pointing to the same folder in my apache2 conf.
and the main domain is "workers.org"
what i want to obtain is basically to have plumbers.org pointing to workers.org/index.php?version=1
and to have cleaning.org pointing to workers.org/index.php?version=2
is something I set in my .htacess? what's best practice?
You can try to add a permanent redirect in the original httpd / apache conf file, e.g.:
<VirtualHost ...>
ServerName plumbers.org
Redirect 301 / http://workers.org/index.php?version=1
</VirtualHost>
...
<VirtualHost ...>
ServerName cleaning.org
Redirect 301 / http://workers.org/index.php?version=2
</VirtualHost>
Technically, it should also work if you put these changes in the corresponding .htaccess files (if .htaccess files are not prohibited by the "parent" conf). Without <VirtualHost> section, of course, then.
Assuming all 3 domains point to the same DocumentRoot folder, you can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)plumbers\.org$ [NC]
RewriteRule ^/?$ index.php?version=1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)cleaning\.org$ [NC]
RewriteRule ^/?$ index.php?version=2 [L,QSA]

Issues Removing index.php from Mediawiki URL

I have a php website (laravel) that is set to the root of a subdomain using a virtualhost. So http://subdomain.website.com will go to the laravel website. I want to be able to go to http://subdomain.website.com/wiki and it will direct to the mediawiki install. To do this is setup an alias within the virtualhost.
<VirtualHost *:80>
DocumentRoot "/var/www/laravel/public"
ServerName sub.domain.com
Alias /wiki "/var/www/mediawiki"
Alias /w "/var/www/mediawiki"
<Directory "/var/www/laravel/public">
AllowOverride All
Options +FollowSymLinks
RewriteEngine On
</Directory>
</VirtualHost>
This is working exactly the way i want it to work, but there is an issue. I want to remove index.php from the url. So that "/wiki/index.php/Main_Page" becomes "/wiki/Main_Page".
I tried using http://shorturls.redwerks.org/ which i found in other guides, but it isnt working for me. Any code generated will produce wiki/wiki/Main_page and even at that the page throws an error.
The requested URL /wiki/wiki/Main_Page was not found on this server.
I have tried messing around with a large amount of different combinations with the url and cant get this to work. Since this website is a subdomain im not able to use wiki.domain.com. This wiki needs to be part of the subdomain. Anyone know what i need to do to make this work?
The end result i need is for this URL to bring me to the main page
http://sub.domain.com/wiki/Main_Page
You need to have mod_rewrite enabled and then you can use rules like this to remove index.php. You can use this in your .htaccess in Wiki folder.
RewriteEngine On
RewriteBase /wiki
#redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.*)index\.php
RewriteRule ^ %1? [R=301,L]
#internally rewrite request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Update: Ordinarily this should work for typical CMS' however mediawiki itself has a unique because of the using wiki and w folders.
http://www.mediawiki.org/wiki/Manual%3aShort_URL/Apache
Depending on the mediawiki structure chosen, it would require a rule such as this
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

301 redirect to same name file, same server, but differnet domain name

I recently did an overhaul of a website in which we switched to a new master domain name for that website. The old domain is still active, and needed to be redirected to the new domain name.
The site still has the same root folder, and so there are a couple of files that were updated, but have the same name (i.e. index.php, about.php, contact.php).
I manually redirected all the files (via .htaccess) that no longer exist to new files with a manual redirect:
redirect 301 /old_file.php http://www.new_domain.org/new_file.php
However this will not work with the files that have the same name such as "index.php". I can still call the site by the old domain name. How do I properly refer these files so that I don't have a duplicate penalty from google?
I tried this and it did not work:
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old_domain.org$[OR]
RewriteCond %{HTTP_HOST} ^old_domain.org$
RewriteRule ^(.*)$ http://www.new_domain.org/$1 [R=301,L]
Thank You
Clear your browser cache and restart it
Make sure mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.org$ [NC]
RewriteRule ^ http://www.new_domain.org%{REQUEST_URI} [R=301,L]
Take a look at using the canonical tag as an alternative.
http://www.seomoz.org/blog/cross-domain-canonical-the-new-301-whiteboard-friday

.htaccess subdomain redirection to root domain

i just want to ask a quick question about .htaccess.
Here's how my webhosting works with subdomains...
Once i create a subdomain... then they create a folder into the root folder like this...
www.mydomain.com ---> public_html
sub.mydomain.com ---> public_html/sub
What i want to do is... to redirect all request from sub.mydomain.com to www.mydomain.com with some GET variable or something to identify from what subdomain the request is coming from...
So for example... when i get a requests to work like this
http://sub.mydomain.com/myphp.php ---> http://www.mydomain.com/myphp.php?comingfrom=sub
http://sub.mydomain.com/(anyUrl) ---> http://www.mydomain.com/(anyUrl)?comingfrom=sub
I'm also wondering if this would execute some .htaccess redirects present in the main domain...
Hope you guys could help me...
Thanks in advance...
Put a .htaccess with following content into your subdomain folders:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) http://www.mydomain.com$1?comingfrom=%1 [QSA,R=301,L]
(untested, sorry)
EDIT:
you pointed out that you would like to keep the subdomain in your addressbar and don't want a redirect. So you need to make a view changes to your <VirtualHost> of www.mydomain.com like so
<VirtualHost ...:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
ServerAlias sub.mydomain.com
DocumentRoot /path/to/your/docroot/of/www.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) $1?comingfrom=%1 [QSA,PT,L]
</VirtualHost>

How to serve content from different directory for sub domains

How to serve contents from different directory for virtual sub domains.
Ex: http://www.website.com gets its content from www directory.
for virtual.company.com i want to serve content from /www/application directory.
And i want to keep the url parameter same.
I have already created php files to display custom message based on the sub-domain., But i am having problems with htaccess,
In simple i just want to load content for subdomains from different directory other than the root directory.
Try this:
RewriteCond %{HTTP_HOST} !^www\.company\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.company\.com$ [NC]
RewriteRule ^(.*)$ /application/%1 [QSA,L]
That doesn´t sound like a very good idea as people could enter http://www.website.com/application and that might lead to unwanted results. I would recommend setting up a virtual host for virtual.company.com and separate the two.
However, it is possible and it would be something like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^virtual\.company\.com$ [NC]
RewriteRule ^(.*)$ http://www.company.com/application/$1 [R=301,L]
Setup a named virtual host.
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot "/path/to/sub/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName another.domain.com
DocumentRoot "/path/to/another/sub/domain"
</VirtualHost>

Categories