WP Site not working properly without www prefix - php

So depending on what I set the WP Settings URLs to, the site either only works properly with the www prefix or without.
Working properly with www prefix
Without www prefix
Changing the Wordpress Address and Site Address to the address without www prefix and the problem is the other way around. (blendpunkt.at is working but not www.blendpunkt.at)

You have to update the domain to use the variant without www using plugins like Better Search Replace, Velvet Blues Update URLs and the general settings of WordPress in the backend (Home and Site URL).
https://wordpress.org/plugins/velvet-blues-update-urls/
https://wordpress.org/plugins/better-search-replace/
Then you have to update https://wwwblendpunkt.at to https://blendpunkt.at or opposite. And also check if there are http variants.
You might also want to enforce https (using HSTS or another solution, see also https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess and the sections for https + www redirection and enforcing https).
Please create a full backup before using these plugins
Because as you can see www.example.com and example.com are different hostnames and cause a CORS (Cross-Origin Resource Sharing) error.
PS: This question would fit better on https://wordpress.stackexchange.com/

This seems to be some kind of a work-around and there might be other solutions. But in the way you ask, it does not matter for you if you use the url with or withour prefix, you just want the map to work. So it is an option to redirect all requests without www to the request with the www prefix.
Change your wordpress and site url to your domain with the www prefix.
In the root folder of wordpress when accessing it with FTP, there should be a .htaccess file. If there is no such file, you can create one (https://wordpress.org/support/article/htaccess/).
Inside of this file, right under the RewriteEngine on you insert this code:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
This way all requests without https and/or without www will be redirect to https://www.
Another way, without having effect on the whole website, you can just create a redirect for this specific page. Adjust this code snippet to your domain and put it into the functions.php file of your theme, or alternatively create a plugin for redirects, if you have multiple of them.
add_action( 'template_redirect', function() {
if( ( is_page('kontakt') ) ) {
wp_redirect( 'https://www.yoursite.com/kontakt' );
exit();
}
});

Related

How to correctly setup Apache redirects for images

I've set up a reverse proxy from my Windows server to a blog hosted elsewhere. All is fine except for the sitemaps.
The blog is on a subdomain: http://blog.example.com
The proxied domain is https://example.com/blog
As I'm using Wordpress, I've opted for Yoast SEO, but despite ARR doing the rerouting Google tools still complains about images it cannot access - on the origin domain. This is correct in one sense because I've added a second robots.txt on the subdomain, to stop duplicate content, but it doesn't make sense, in the sense that Application Request Routing should be hiding the subdomain. However, we all know that Google does what it wants to do.
I've found some code which I've added to my htaccess file:
# WordPress SEO - XML Sitemap Rewrite Fix - for reverse proxy
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ https://example.com/blog/index.php?sitemap=1 [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ https://example.com/blog/index.php?sitemap=$1&sitemap_n=$2 [L]
# END WordPress SEO - XML Sitemap Rewrite Fix
I'm not sure whether it's doing anything at the moment because the image issue still exists, so my next step would be to try and redirect images to the new domain structure... and herein lies the problem - I know absolutely nothing about Apache stuff and definitely not apache rewriting.
What I need to do is redirect anything in the uploads folder, to a new absolute path
From, /wp-content/uploads/myimage.jpg to https://example.com/wp-content/uploads/myimage.jpg
Can anyone help with this final piece of the jigsaw?
Thanks in advance.
You can probably use something like the following in your .htaccess:
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/
RewriteRule ^(.*)\.(jpe?g|gif|png|bmp)$ https://example.com/wp-content/uploads/$1\.$2 [NC,L,R=302]

Point subdomain to root without using a subdirectory

I have the following scenario:
blog.site.com points to server1
site.com points to server2.
I want blog.site.com to become site.com/blog. So I thought I'll point blog.site.com to server2 and put in a subfolder blog where I can place an .htaccess file doing the 301 redirect. However by creating a blog subdirectory it overrides the /blog/title-goes-here with a blank directory.
My problem is server1 is being decommissioned and so it won't be available to be used for the 301 redirect.
Is there a way I can point blog.site.com to server2to become site.com/blog/title-goes-here without using a blog subdirectory? Or using a blog subdirectory but not interfering with the url?
Please note. This is a wordpress website which the posts from serve1 have been imported. It's important to keep the blog as a part of the site.com website.
Unless I'm missing something, you don't need a redirect - you need rewrite rules.
Have your DNS entries for blog.site.com and site.com point to server2. Then in the .htaccess on server2 add the corresponding rules:
RewriteCond %{HTTP_HOST} ^blog\.site\.com$
RewriteRule (.*) /blog/$1 [L]
Now whether you access site.com or blog.site.com, you'll end up accessing the same server. The rewrite rule will then ensure that requests of type blog.site.com/something actually call site.com/blog/something.
Note that I haven't tested this though.

htaccess rules html to php

I want to be able to redirect folders and subfolders (all html) to another server with different folders (php)
I have a domain with DNS redirects Cname and A field pointing the domiain (that has not changed) to new website using php.
So, I want to do the following as an example.
On the old server
http://www.domain.co.uk/folder1/subfolder1/page.html
to go to
New server
http://www.domain.co.uk/folder3/
The domain used to point at the htdocs folder on the first server but now using CNAME and an A field it points to the second (new) server
Can I just redirect bulk folders to the new folders?
Hope this makes sense
Richard
You can redirect bulk folders.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]
This is a simple code that redirects ftm domain.com to otherdomain.com. But if you want to change folder1/folder2 to folder3, well.... just think you could you possibly shrink that to a simple rule? I think below 50 pages you better off wiriting 301 redirects line by line. Otherwise, no short way.

Using 404 page as redirect system?

I have to move a entire site to a new domain, the old domain will be used for another site, so I wish delete all files in old domain.
Can I delete all file and use 404 error page to redirect traffic to new pages?
example:
if($page == "http://www.site1.com/pagexxx.htm") {
header( 'Location: http://www.site2.com/new_page.html' ) ;
}
It technically and SEO correct?
Another method is to create another php file on your old domain, and in .htaccess add the following:
ErrorDocument 404 /dir/.../file.php
You just need to get the request uri in php and process the url and use the header function to redirect.
If you are doing 1:1 redirection, then just setup a redirect rule on your web server.
If, instead, you want to inform users that the site as moved, and redirect everything to the front page of the new site, I'd do the following:
1.) Create a redirect/rewrite rule on the web server to direct everything to your "This site has moved" page. I'd prefer a rewrite, so that they can easily change existing bookmarks.
2.) Put a message informing them of the move, a link to the new site, and a meta-refresh to automatically redirect after 5-10 seconds.
I would try it in .htaccess create or update an .htaccess file and put it in the root of the old domain with this below. If you are just changing domain names and files locations are still the same, this will redirect all traffic from old to new.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

How to build a subdomain from script

In my project I have to make a subdomain, i.e
if the user name is XXX when he register, a sub domain will be created like XXX.example.com
how to do it?
I will use php for scripting.
I found a script that seems to do exactly that, create a subdomain on your server on demand.
It probably needs a little bit of tweaking for it to work on your particular control panel, but the review are quite positive as far as I can tell.
Link
Have you considered using htaccess and url rewriting?
Found this code that may help you:
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
Source (Post #6)
This might be a little more complex than you think.
I suggest to do some reading on mod rewriting and htaccess.
You could start here:
htaccess Tutorial
Modrewrite tutorial
Subdomain Modrewrite Example
EDIT: Or just go with one of the nice examples provided my fellow SO users. ;)
As long as this is for non-SSL sites, then by far the easiest way is not to bother - just use a wildcard DNS domain and vhost, then map any domain specific behaviours in your PHP code. If you need SSL sites then its a lot more complicated - you need to have a seperate IP address/port for each certificate - and woldcard certs can be very expensive.
If you're wanting to set up some sort of hosting package then its a bit more involved - how you go about this depends on what webserver and DNS server you are using.
Assuming (again no SSL) with Apache on Unix/POSIX/Linux and bind, then, again I'd go with a wildcard DNS entry, then:
1) create a base dir for the website, optionally populate this with a default set of files
2) add a vhost definition in its own file in /etc/httpd/conf.d named as XXX.conf
3) send a kill -HUP to the HTTPD process (causes it to read the new config files without having to do a full restart).
One thing to note is that you really shouldn't allow the httpd process direct write access to its own config files - you definitely don't want to give it root privileges. A safer solution would be to create a CLI script to perform this using the username as an argument then make it setuid and invoke it from the script run by the HTTPD process.
C.
the best way is to use a joker in your DNS server :
www.example.com. IN A 1.2.3.4
*.example.com. IN A 1.2.3.4
By this way, No subdomain has to be created : all are pointing to the same IP by default.
In your PHP code, you just have get $_SERVER["HOST"] and get the fist part :
$hostParts=explode('.',$_SERVER["HTTP_HOST"]);
$user=$hostParts[0]
First, you need to make sure you have a wildcard domain setup in DNS, and make sure your webserver (apache?) directs all queries for that wildcard domain to your php file.
Then in php you can look at $_SERVER["HTTP_HOST"] to see which subdomain is used for that particular request.
Since you will make sub-domains when an user registers.
Try this as .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST}!^www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
RewriteRule ^$(.*) /$1/%1 [L]
make a function of a controller which will take the value of sub-domain and display what necessary.
like::
public function show ($domain)
{
**.**..*..**.** Your code goes here
}
when a user will try this xxx.domain.com/controller/show this will be domain.com/controller/show/xxx . if you want to xxx.domain.com to be domain.com/controller/show/xxx just edit the htaccess file as you want.

Categories