rewrite Wordpress subdomain with nginx and htaccess - php

I got a domain ihakula.com, and I want to use blog.ihakula.com to point to my blog with wordpress.
And the path for wordpress is www.ihakula.com:9000/blog/, but I want in address bar which looks like no1.ihakula.com.
Now I use Nginx and .htaccess to made it, seems I got problems.
Here is the Nginx code:
# For ihakula blog
server {
listen 80;
server_name no1.ihakula.com;
location / {
proxy_pass http://www.ihakula.com:9000/blog/;
}
}
Here is the htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.ihakula\.com\:9000\/blog\/$
RewriteRule ^/?$ "http\:\/\/no1\.ihakula\.com\/" [P,L]
</IfModule>
Now I removed .htaccess file and changed the wordpress general config looks like this:
I can reach my wordpress console start with no1.ihakula.com/wp-admin. But when I hover any Post, the link shows below still start with www.ihakula.com:9000 looks like below:
Any ideas?

No, you do not need to do anything in your htaccess file. NGINX proxy can handle the request URL change automatically. What you are doing is actually making the sites to undertake infinite redirects.
What you should do more is to set the BLOG URL in WordPress to no1.ihakula.com, so that WordPress scripts will automatically give the URLs begins with no1.ihakula.com.

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]

Set .htaccess at localhost and hide index.php

I wanna ask about .htaccess that could be change the url to be more simple. I have the url in my localhost like this:
localhost/spsb/index.php
and
localhost/spsb/index.php?page=student
The question is:
How can i change the url above being:
localhost/spsb
and
localhost/spsb/student
And where is the correct place to save the .htaccess file?
Thank's a lot for advice
I have been searching a lot everywhere but no one can solve my problem
Use URL Rewriting
For an Apache server:
First, you must activate the rewrite module
(How to active mod_rewrite)
Add those lines to your htaccess to enable rewrite engine
Options +FollowSymlinks
RewriteEngine On
And this line to rewrite the url as you want
RewriteRule ^spsb\/?$ spsb/index.php
RewriteRule ^spsb/([a-zA-Z]+)$ spsb/index.php?page=$1 [L]
For a Nginx server:
Put these lines in "Location" scope:
if (!-e $request_filename)
{
rewrite ^spsb\/(.*)$ spsb/index.php?page=$1;
}
Note: the location of .htaccess is relative to his level into the site structure, if he is inside the first level site/dir/ the contained rules are limited to site/dir structure
To be unlimited, i suggest to put it into the document root
...Or you can put the rules directly into you vhost configuration
How to check errors:
First have you restarted your server to apply the activated rewrite
modules
(rules from htaccess dont require a restart, only module
modification)
Add following lines to your htaccess after "RewriteEngine On":
RewriteLog "/var/log/apache2/{your_log_site...}" <-- put your path
RewriteLogLevel 7
Check by a "tail -f /var/log/apache2/you_log_site.errorlog" to see if
an error appear at resfresh page

Redirect Subdomain to Subdirectory without changing URL multiple WordPress

I have my folders in public_html like this,
/ - root public html for example.com [has wordpress in it]
/a/ - this has another wordpress installed and a.example.com should be pointed to this directory
/b/ - again wordpress for b.example.com
I tried this code,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^a\.example\.com$
RewriteCond %{REQUEST_URI} !^/a/
RewriteRule (.*) /a/$1
but it doesn't work. I use VestaCP control panel and don't want to add site for each subdomain instead have it all under the same root directory.
I also noticed that going to a 404 page like example.com/a/somenotfoundpage will show me the 404 page of the root wordpress installation and not /a/ installation
How can it be fixed inside .htaccess ? /a/ and /b/ are both multi site installation each and / is normal installation
the url for the /a/ and /b/ are set to a.example and b.example in wordpress settings
EDIT
VestaCP is set to redirect *.example.com to example.com. (I have used it as an aliases when adding the site, and visiting a.example.com shows me example.com content without the address changing in the address bar)
In your wp-config.php file, there's a line that says Subdomains and it's set to 'false' right now. Change it to true and your site will flip over.
Now that said... You may want to consider .htaccess redirecting the old URLs to the new ones. But it should work just that easily.

one subdomain as cname for another domain, can i have different custom 404 pages

Actually I have a domain - domainone.com
I have created a subdomain cname of anoter domain as
abc.domaintwo.com - CNAME as - domainone.com
so that I can use the js and css files e.g domainone.com/js/jquery.js files as abc.domaintwo.com/js/jquery.js
SO Far everything is FINE, no issues at all.
Problem:
I have a custom 404 page for domainone.com and now when that abc.domaintwo.com goes 404 same page appears which i don't want. Any help is appreciated.
Htaccess of domainone.com:
ErrorDocument 404 /404/
All i want is to have different 404 for both.....
Notes: Don't have access to shell for symlinks or alias as I am on shared hosting.
For what you are trying to do, you will need to use PHP. It cannot be done in your .htaccess. The ideal place would be the virtual hosts file, but sounds like you don't have access.
You should separate the web roots apart from each other, and then mod rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} abc.domaintwo.com$ [NC]
RewriteRule ^(js|css)/(.*) http://www.domain.com/$1/$2 [L,PT]
This should redirect http://abc.domaintwo.com/js/* and css/* to www.domain.com/js and css/
Note: it is untested, but should work if my memory serves correctly.

Redirect Large Number of URLs (HTML Files) to Wordpress

I have over 2000 HTML files that are now in Wordpress blog. I have the URL Map of Old_file.html and new wordpress URL.
I want 301 redirect but don't want to add 2000 lines to htaccess. Can you please suggest how to accomplish this using PHP so that when there is a request for old url, the php script should lookup into the database and redirect(301) to the new URL ?
Thanks.
You can make your map a mod_rewrite rewrite map like this:
# old new
Old_file new-url
Then you just need to register the rewrite map in the server or virtual host configuration:
RewriteMap examplemap txt:/path/to/file/map.txt
And finally set a rule that does the redirect (either in your server/virtual host configuration or an .htaccess file):
RewriteCond %{examplemap:$1} .+
RewriteRule ^/?(.+)\.html$ /blog/%0 [L,R=301]

Categories