How to add www. to requesting URL on a Drupal site? - http

I'd like to redirect, for example:
http://example.com/ -> http//www.example.com/
http://example.com/node/1 -> http://www.example.com/node/1
https://example.com/cart -> https://www.example.com/cart
Thanks!

In the root directory of your Drupal site, check out the comments in the .htaccess file (in the # Various rewrite rules section. Specifically, look for the following lines:
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
... and uncomment those last two, replacing example.com with the appropriate domain name.

Related

Redirect all pages from one domain to current domain

I have multiple subdomains, for example:
uk.domain.com
us.domain.com
eu.domain.com
And I need to redirect all of these subdomains (and any pages that the user tries to access on those subdomains) to:
www.domain.com
Example:
uk.domain.com/page1 -> www.domain.com/page1
uk.domain.com/page2 -> www.domain.com/page2
us.domain.com/page1 -> www.domain.com/page1
us.domain.com/page2 -> www.domain.com/page2
eu.domain.com/page1 -> www.domain.com/page1
eu.domain.com/page2 -> www.domain.com/page2
...etc
Is there a rule that can apply to all URLs on each of the subdomains and redirect them to the main domain, with the page in the URL intact, as above?
I have tried example here, but that did not work
Solved. This works
RewriteCond %{HTTP_HOST} ^uk\.something\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?$ [NC]
RewriteRule ^(.*)$ https://www.somethingelse.com/$1 [R=301,L]
However it needed to be at the top of my .htaccess, since I had other rules that were interfering with it.

Redirect domain.org to www.domain.org

I have the following doubt.
On a Debian server with apache I have a wordpress published with the domain "mydomain.org".
I want to be able to do the following:
When in the browser I write http://mydomain.tld it redirects me to http://www.mydomain.tld, that is, redirect a non-www request to www request.
When accessing http://mydomain.tld, what appears on the screen is the "Locked Page" message from the firewall in front of us, so I understand that the problem itself is on the side of the firewall in the configuration itself of the WAF.
Can it be correct?
Thank you very much in advance
1) Create an .htaccess file in your root directory where wordpress exist. If you already have an .htaccess file in the root directory, you can modify it.
2) Copy and paste the following lines of text into the .htaccess file: paste any one code as per your requirements.
#Remove www from any URLs that have them: Paste any one code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Add www to any URLs that do not have them: paste any one code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3) Replace example.com in the final line of text with your own domain name.
4) Save the .htaccess file.
5) To test the configuration, use your web browser to visit http://www.example.com, where example.com represents your domain name. The browser should be redirected to http://example.com.

How can I change www.mysymfonypage.com/public to www.mysymfonypage.com?

When I locally open the url localhost:8000 then I see my main homepage.
I installed the apache pack (composer require symfony/apache-pack) and uploaded everything via ftp on my server.
This is the .htaccess file in my public folder:
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks
# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
I open www.mysymfonypage.com and it redirects to the public folder, which is fine. But my URL is now www.mysymfonypage.com/public
How can I change this?
Really you should reconfigure your webserver to use /public as webroot - How do I change the root directory of an apache server?
Alternatively you could add
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]
after RewriteEngine On - BUT this will 301 redirect all your requests so that would likely cause issues / undesired behaviour somewhere along the line.

Installed Wordpress Network with www in the Site URL. How do I fix this?

I made a huge mistake. I set up a network without changing sitename to non-www, so now example.com (without www) is a non-existing page. How do I fix this? Changes in
settings
DNS
htaccess
?
I've tried htaccess redirect but wordpress sees the first request and still says www is missing.
We can't edit the Wordpress source code to redirect, as it will be broken on future updates. We can't forward all requests to the www-version of that request as that will break all subdomains.
I solved this with some edits to the root .htaccess file.
# This is probably how your file starts already
RewriteEngine On
RewriteBase /
# Then you add a condition: if the host starts with example.com
RewriteCond %{HTTP_HOST} ^example.com.*$
# And add a rule: redirect that url to the same url just with prepended with www
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Here the file continues with other stuff from WP
RewriteRule ^index\.php$ - [L]

Force Redirect www.domain.com\blog to domain.com\blog .htaccess?

I have a wordpress Multisite instance that for some reason defaults to domain.com (/blog with no www in front. Any request with a www in front gets forced to the home page (domain.com/blog) and not to it's intended page of say domain.com/blog/page.
What I am trying to do is force all incoming traffic to strip off www if it exists and just go to /blog/what-ever-page-youre-trying-to-go-to. I need it to go to the page that is requested, currently if you have anything on the end of the /blog URL (/blog/ANY-PAGE-HERE) it defaults to /blog.
Anyone have any idea how I would go about fixing this? In .htaccess seems to be the easiest way but I can't seem to find a viable solution after working on it for a couple of hours.
PS: it's multisite so I cannot just go into setting and change the URL and HOME to www.domain/blog
This is what I use on my sites:
<IfModule mod_rewrite.c>
RewriteEngine on
# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]
</IfModule>
Add this above all your wordpress stuff in the htaccess of your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.net$ [NC]
RewriteRule ^blog(.*) http://domain.net/blog$1 [L,R=301]

Resources