I am having some issues with URL rewrite from sub domain to sub directory of a wordpress site. I didnt set the WP stuff up just inherited the problem. The box is a Bitnami WordPress.
I am attempting to do some redirects with url masking in the vhosts config. The redirects are fine just not getting the URL to mask and ending up with ugly sub dir in the URL.
This is what I have so far in /opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Ugly_sub_dir [P]
RewriteCond %{HTTP_HOST} ^app\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Another_ugly_sub_dir [P]
And in both cases I am redirecting fine but ending up with URL as:
domain.com/Ugly_sub_dir
domain.com/Another_ugly_sub_dir
But would like to see the plain old..
www.domain.com
app.domain.com
Any help much appreciated
OK I was approaching this in entirely the wrong way. Solved by setting up a proper vhost (not sure why I didnt do this in the first place - wasnt thinking, its been a while) ..
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs/Ugly_sub_dir"
Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>
Related
Please let me know how I can redirect http domain to https domain hassle free via htaccess file. I want to redirect this domain
http://example.in to https://example.com
domain.
Please help me out.
Thanks in advance!!!
Apache doesn’t recommend to use this in the .htaccess, you can read about it here.
You should use this instead:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
In case that you want to use .htaccess anyway you need to use the next rewrite:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Previously I had a website implemented as a single page application on S3, and it had a page pointing to a list of tools. The URL for this list was: example.com/tool/<tool-name>
To expand the features of my website, I redid the DNS so that this old site is now sitting on a subdomain: tools.example.com/tool/<tool-name> (the new site, that is, example.com, is now on Lighthouse/Wordpress).
As there are still a bunch of links around the web referencing URLs in the tool list of the old site, I would like to write a redirect on Bitnami.
I've already asked for help on this and was told to do the following, but the redirect still doesn't seem to work when I go to example.com/tool/<tool-name>:
Modify /opt/bitnami/apps/wordpress/conf/httpd-app.conf by adding the following rule to the bottom of the file:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/tool/(.*)$
RewriteRule ^(.*)$ https://tools.example.com/tool/$1 [P,L]
sudo /opt/bitnami/ctlscript.sh apache restart
Is this pattern correct, or am I writing to the right file?
EDIT
I changed the redirect code above slightly to reflect feedback in the comments. It is redirecting to https not http and I changed the last bit of code to [P,L] from [R=301,L]
I just tested this change in the Bitnami solution and it worked properly, can you try it?
Remove the changes from the /opt/bitnami/apps/wordpress/conf/httpd-app.conf file
Edit the /opt/bitnami/apache2/conf/bitnami/bitnami.conf file and include the Rewrite* lines the VirtualHost of the port 80
...
<VirtualHost _default_:80>
DocumentRoot "/opt/bitnami/apache2/htdocs"
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/tool/(.*)$
RewriteRule ^(.*)$ http://google.com [P,L]
...
You can also add those lines in the VirtualHost block of the port 443 in the same file.
Restart Apache
sudo /opt/bitnami/ctlscript.sh restart apache
As you can see, in my example, Apache is redirecting to google.com but you can redirect to any URL.
I want, when open subdomain to return existing Wordpress page ex. www.example.com/custom-wordpress-page.
Server OS is CentOS 6.6, while Web Server (httpd) is Apache 2.2.15.
It have installed and configured Wordpress with .htaccess files configured for it's mod_rewrite.
Root folder is /var/www/html.
.htaccess file is in /var/www/html/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This configuration is working very well with one domain (ex. example.com). Today I wanted to add subdomain (ex. subdomain.example.com).
I tried multiple solutions including VirtualHosts with/without NameVirtualHost in httpd.conf file, also adding RewrriteCond&RewriteRule
under mod_rewrite in .htaccess file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Redirect permanent / http://example.com/custom-wordpress-page
</VirtualHost>
Also in first VirtualHost, I tried *.example.com instead off www.example.com.
In second one, I tried to make Reverse Proxy using ProxyPass and ProxyPassReverse.
Instead of using :*80 I tried using IP address of server and domain name.
Using .htaccess Rewrite rule with/without VirtualHosts gave no results.
subdomain.example.com always opens www.example.com instead of www.example.com/custom-wordpress-page.
Keep in mind - there is a lot of threads like this, but I tried many of them, including many tutorials but they aren't working alongside Wordpress.
Edit : Want to add that there are no wildcards in DNS records. There are NS records, one A record for example.com, one A record for subdomain.example.com and CNAME record for www that is poiting to example.com. There are NO *.
There was problem with hosting provider DNS records, configuration was correct, but I had to call provider to check records in their system and fix what is broken. After they done their job, it works correctly.
tl; dr
I have a Magento install on www.example.com
I have a Wordpress install on www.example.com/wordpress
And I need the following urls to also serve that same wordpress content;
www.example.com/eu/wordpress
www.example.com/gb/wordpress
P.S.: I know there are duplicate content issues with this, please ignore that
The question is: What's the best way to do that?
The full story
I have a Magento multi store site using the 2 digit language code subdirectory technique.
I have one Wordpress installation in it's own subdirectory.
app
downloader
errors
eu/ - symlinks for the € Euro store
gb/ - symlinks for the £ UK store
includes
js
lib
media
shell
wordpress/ - The Wordpress install
var
I need the Wordpress blog to be available from all stores so they user stays in the store with their locale/currency.
What I have Tried
Using the answers in these Stacks;
htaccess multi language site with sub directories, and default 301
Endless Redirect Loop by htaccess rules multi language
I've made attempts but unfortunately I am terrible with .htaccess and vhosts problems
Via the vhosts file
<VirtualHost *:80>
ServerName www.example.com/eu/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com/gb/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
Via the Wordpress htaccess
RewriteCond %{REQUEST_URI} !^/(eu|gb )/wordpress(/|$) [NC]
RewriteRule ^(.*)$ wordpress/$1 [R=301,L]
Via the Magento .htaccess
RewriteCond %{REQUEST_URI} ^/eu/wordpress/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/gb/wordpress/(.*)$ [OR]
RewriteRule ^/wordpress/.*$ - [L]
Firstly, you don't need vhosts for wordpress, you need only one vhost per domain and/or subdomains. Your vhost should look something like this(which I assume that you already have a vhost similar to this for your magento shop):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/vhosts/www.example.com/public
<Directory "/var/www/vhosts/www.example.com/public">
# allow .htaccess files to override all directives
AllowOverride All
</Directory>
</VirtualHost>
Now, you just need to modify magento's .htaccess(/var/www/vhosts/www.example.com/public/.htaccess), and add the following rules:
<IfModule mod_rewrite.c>
RewriteEngine on
# rewrite rule to redirect
# eu/wordpress -> /wordpress/
# eu/wordpress/ -> /wordpress/
# gb/wordpress -> /wordpress/
# gb/wordpress/ -> /wordpress/
RewriteRule ^(eu|gb)/wordpress/?$ /wordpress/ [R=301,NC,L]
# ... continue here with magento's rewrite rules ...
</IfModule>
We have this in production, hopefully this answer might help someone at some point.
The way we've done this is to add a RUN_CODE environmental variable which is used in a custom Wordpress filter to get the urls all working. I've used the 'eu' example from my question to illustrate it below. Note we had to do this for both Apache and Nginx so I've added the .htaccess and server blocks for both.
APACHE - .htaccess
In the eu country stub subdirectory you add this to your .htaccess file (so eu/.htaccess)
RewriteCond %{REQUEST_URI} ^/eu/wordpress$
RewriteRule ^(.*)$ /eu/wordpress/ [R=301]
RewriteCond %{REQUEST_URI} ^/eu/wordpress(.*)$
RewriteRule ^(.*)$ /wordpress/index.php/$1 [L,E=RUN_CODE:eu]
NGINX - Server Block
location ~* /eu/wordpress(.*) {
if (!-f $request_filename) {
set $code 'eu';
rewrite ^(.*)$ /wordpress/index.php?$1 last;
break;
}
}
In the Wordpress root index.php this was added at the top of the file
$pos = strpos($_SERVER['REQUEST_URI'], '/wordpress');
if ($pos !== 0) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], $pos);
}
Wordpress theme functions.php
add_filter('post_link', 'link_mcnab');
add_filter('page_link', 'link_mcnab');
add_filter('bloginfo_url', 'link_mcnab');
function link_mcnab($link)
{
if (isset($_SERVER['REDIRECT_RUN_CODE']) && $_SERVER['REDIRECT_RUN_CODE']) {
$homeUrl = home_url();
$domain = substr($homeUrl, 0, strrpos($homeUrl, '/'));
$link = str_replace($domain, $domain . '/' . $_SERVER['REDIRECT_RUN_CODE'], $link);
}
return $link;
}
My main website is located on www.example.com. On the subdomain sub.example.com I'm building another website. Both websites are build with WordPress. Both domains are registred at the same registrar
I registered a domainname (lets say: www.example2.com) wich I would like to point to sub.example.com. When someone visites www.example2.com he/she should see www.example2.com in the address bar and not sub.example.com.
I was trying to get this to work with a .htaccess file and it kind of worked, but not in the way I was hoping:
when I enter example2.com the browser opens sub.example.com and in the address bar it states example2.com (so far, so good)
when I navigate to another page on the website the address bar shows sub.example.com/another-page instead of example2.com/another-page
when I enter www.example2.com the browser opens www.example.com
This is how my .htaccess file looks like at the moment:
RewriteEngine On
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteCond %{HTTP_HOST} www.example2.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com [R=301,L]
Can someone tell me how I can get this to work?
Try this and see if it works for you. Also I would make sure the www dns record for example2.com is pointing to the same location as example2.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [L]
Edit:
Really for what you are trying to accomplish, I would suggest NameBased virtual Hosts if possible. That's what it was designed for. So that you can use the same IP for multiple sites. This change is done in the apache config file. See this simple example.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /www/example.com/html
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
ServerAlias example2.com
DocumentRoot /www/example.com/html/sub
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/name-based.html
That tells the server what document root to use for each website and therefore eliminates the need for these mod redirects and CNAMEs to sub domains.