.htaccess method of setting a default URL to resolve to? - php

We have two sites, and we've just now made an in-house development server that we want to have both sites on. What I want to accomplish is to have an .htaccess in each of the site folders that prepends the development URL of the site. For example, where they simply have a header(location: /folder/whatever.php), the files should actually go to thesite/folder/whatever.php.
Currently what happens is (for example) they'll go to 192.168.x.x/folder/whatever.php

If I understood correctly and what you want is to redirect 192.168.x.x/folder/whatever.php to thesite/folder/whatever.php, you could use this:
RewriteCond %{HTTP_HOST} !^thesite.com$
RewriteRule ^(.*)$ http://thesite.com/$1 [R=301,L]

Related

RewriteRule Not Working in .htaccess file

I'm trying to change the URL of my website to show only the ID but It seems to not work...
I don't know why.. other commands of RewriteRule work well..
Actually the .htaccess file looks like belove
RewriteEngine On
RewriteRule ^/?([0-9a-zA-Z-]+)/$ article.php?articleId=$1 [L]
I want that it works like this:
Old_URL(to modify):
mywebsite.it/article.php?articleId=15
I want something like this:mywebsite.it/article/15
But the URL remains the same actually: always display this: mywebsite.it/article.php?articleId=15
Thanks in advance to every help :)
An internal rewrite will never change the URL visible in the browser. You are probably looking for an external redirection. Or better the combination of both:
RewriteEngine On
# externally redirect old URL
RewriteCond %{QUERY_STRING} (?:^|&)articleId=(\d+)(?:&|$)
RewriteRule ^/?article/?$ /article/%1 [R=301,L]
# internally rewrite new URL
RewriteRule ^/?article/(\d+)/?$ /article.php?articleId=$1 [END]
Those rules are meant to be implemented on top level. Best in the actual http server's host configuration. If you do not have access to that then a distributed configuration file will work (".htaccess") when located in the host's DOCUMENT_ROOT, but support for that needs to be enabled.
It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are happy with how things work. That prevents caching issues on the client side.

How to exclude subdomain from htaccess changes

I have this site lets say (mydomain.com) that has been running for quite a while, we decided that we would like to build a blog too, naturally we wanted to use wordpress(great for blogs). We wanted to use a subdomain blog.mydomain.com
mydomain.com uses cloudflare and the site itself is build with Laravel, our project is placed in public_html/coolproject because of laravel we had to make these changes to our htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ coolproject/public/$1 [L]
</IfModule>
After deciding to create a blog we naturally created A record in cloudflare pointing to our ip just like the main domain. (blog.mydomain.com)
The issue is following: every time we try to access blog.mydomain.com it shows mydomain.com content.
I think that the issue lies in
RewriteRule ^(.*)$ coolproject/public/$1 [L]
because every request is pointed to coolproject/public folder.
I am not very good with htaccess and don't know all of it's possibilities, I am wondering if it's possible to edit this rule so subdomains wouldn't be affected in this scenario and how.
goal is for blog.mydomain.com to show public_html/mycoolblog contents.

.htaccess redirect from subdomain to subdomain

A new client needs my help, their web developer messed up - built website on a draft/test server but forgot to block Google etc. I would appreciate help for the community here, I am not an expert with HTACCESS redirection.
As I said, another website developer setup the clients draft site on their draft server, its been there for months, however they forgot to hide it from search engines, so the content has been indexed by Google etc – this will trigger a duplicate content penalty if web put the new website live and the new website will be useless effectively.
I have access to the draft site / server and can modify the HTACCESS file, so when the new site goes live I would like to have the correct redirects in place. There are a few subdomains on the site (it's a multi language site), so it's a little tricky.
The website is built on Wordpress
The website structure looks like this on the test server. All files page names and file names are identical, just moving to a new server.
http://clientdomain.testserver.com
http://it.clientdomain.testserver.com
http://fr.clientdomain.testserver.com
http://es.clientdomain.testserver.com
http://de.clientdomain.testserver.com
http://ko.clientdomain.testserver.com
http://pt.clientdomain.testserver.com
http://ru.clientdomain.testserver.com
http://tr.clientdomain.testserver.com
http://cn.clientdomain.testserver.com
The redirects will need to go here:
http://clientdomain.com
http://it.clientdomain.com
http://fr.clientdomain.com
http://es.clientdomain.com
http://de.clientdomain.com
http://ko.clientdomain.com
http://pt.clientdomain.com
http://ru.clientdomain.com
http://tr.clientdomain.com
http://cn.clientdomain.com
The existing HTACCESS file on the test server looks like this
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
I would really appreciate any help on this.
There are some existing threads which contain all the pieces of the HTACCESS puzzle, but I am a little confused:
How can I redirect from one subdomain to another in .htaccess?
How can I redirect from one subdomain to another in .htaccess?
Kind Regards,
GG
If it was me I wouldn't bother messing around with redirects, get the urls removed from the index. Google will remove them with 24 hours, sometimes much quicker nowadays.
Add the development domain to your Webmaster Tools account and verify it. Then go to Google Index -> Remove Urls;
Just enter the the value / in the removal request which tells Google to remove every url in the index for that domain.
Then add a blocking robots.txt file to site root;
User-agent: *
Disallow: /
And what I normally do (this has happened a couple of times to me despite robots.txt and basic auth protection - git disaster/shenanigans) is prompt Google to reindex the site straight away. Go to Crawl -> Fetch as Google
Leave the input box blank so it fetches the whole site and just hit the Fetch button. When Google has fetched it click the 'Submit to Index' button.
You will be amazed how quickly this can happen these days, used to take weeks if you were lucky.
EDIT
And just to make sure this doesn't happen to anyone else finding this, the best way to stop it getting a dev site indexed isn't a robots.txt file or using Basic Auth via the .htaccess file (as previously mentioned it's easy to accidentally delete these). You should enable Basic Auth on the development site via the vhosts file.
Like it's not only for Google...
You can use this .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?:..\.)?clientdomain)\.testserver\.com [NC]
RewriteRule ^ http://%1.com%{REQUEST_URI} [NE,L,R=301]

htaccess redirect to subfolders resources

I have a problem:
I have to modify an application written in Zend 1 that was deployed to the domain root. now to test it the customer gave me a root subfolder called [domain]/social
When I run it, I have a lot of problem because all paths are like "/resource", but in this case the fiddler show me that the request look for [domain]/resource ad not for [domain]/social/resource.
It happens with script sources, ajax urls, hrefs...all!
Is there a way to fix the problem?
I am not quite familiar with Zend, but is there a main config file that sets the application root folder? (Like joomla) You can modify that if it exists.
Other option would be to preg_match or str_replace all instances of the domain name in the code ( like when moving a Worpress site) but you should definately do that with a sample content not couple hundred pages).
And here is a htaccess snippet as well:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/social/$1 [R=301,L]
Be sure to put the htaccess under the development folder not under the root, oterwise you're facing issues.
I suggest cheking for a config file first. Hope one of this helps!

Is there an alternative to PHP's $_SERVER['HTTP_HOST']); for apache that does not require a full blown programming language (e.g. SSI)

I have a simple html page that only uses PHP in two places
<?php preg_replace('/(www\.)?([^.]*)\.(com|info)/', '${2}', $_SERVER['HTTP_HOST']); ?>
<?php echo $_SERVER['HTTP_HOST']); ?>
In page is loaded on multiple domains, and I just want to display the host name as text in some other static content
I'd like to remove the need for PHP completely, but not repalce it with another full blown compiler or interpreter. I'd like to avoid using javascript. I can live without being able to do a regex to get the second level of the domain name, but would still like that option. Do I have any options for doing this via a simpler apache module than mod_php?
Theres nothing wrong with mod_php, I'm just seeing if I can minimalize the needs of this website I am working on.
I’d combine both mod_rewrite and SSI. Set an environment variable with mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.(com|info)$
RewriteRule ^ - [L,E=HOST:%2]
And then access that information in SSI with:
<!--#echo var="HOST" -->
I havn't tested it buy you might be able to use htaccess and do a rewrite like this:
RewriteRule (.*) $1?httm_host=%{HTTP_HOST} [L]
I don't know for sure that the %{HTTP_HOST} variable is available in a rewrite but it may work. You may need to use a condition to check for the ? in the URL.
How about JavaScript? You only need two small changes on the site, that can easily be done. So you can plainly serve static HTML files. And to get the domain, you can use:
var domain = window.location.hostname;
Cheers,
Edit: To use mod_rewrite: You will have to set up two identical HTML files, with the correct domain in each. Then you can deliver them via
RewriteCond %{HTTP_HOST} .+\.com
RewriteRule index.html index.com.html [L]
RewriteCond %{HTTP_HOST} .+\.info
RewriteRule index.html index.info.html [L]

Categories