Load wordpress through specific URL path from subdirectory using htaccess - php

The goal
Visting www.DOMAIN.com/blog to load Wordpress from a directory named "/wp-blog" or similar. Also another rule is the host must match a specific domain.
Current rules
This does not seem to be working:
RewriteCond %{REQUEST_URI} ^blog [NC]
RewriteCond %{HTTP_HOST} ^(www.)?DOMAIN.com$
RewriteRule ^(/)?$ wp-blog [PT,L]
Any ideas what we're doing wrong?

The fix was actually simple
The rules just needed altering slightly and I had to actually change the /blog directory name as some other rules were causing apache to get confused.
RewriteCond %{REQUEST_URI} ^/blog [NC]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^blog(/.*)?$ /wp_blog/$1 [L]
Also had to update htaccess for the Wordpress install as well to reference wp_blog

Related

Htaccess Multi domain, but redirect one domain except a specific path

I have a multi-domain configured htaccess which means depending on what domain name is entered, the depending content is displayed and is redirected to the https version of whatever domain
A small snippet of my what I tried in my .htacess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^arabme.com$ [NC]
RewriteRule ^(.*)$ https://www.arabme.com$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^chiname.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(landing|marketing)
RewriteRule ^(.*)$ https://www.greatwall.com$1 [R=301,L]
but on chiname.com I want to redirect all to http://www.greatwall.com except for two folders. /landing and /marketing.
So whenever a user enters chiname.com/landing/*.php or chiname.com/marketing/*.php it needs to display without redirecting to the https://www.greatwall.com and for every other path regarding chiname.com needs to redirect to https://www.greatwall.com.
My above would always redirect to https://www.greatwall.com/ regardless of my rewriteCond when I access any of the /landing or /marketing folders from chiname.com.
Note I do not have Server level privileges so I do not have access to the VirtualHost .conf files.
Arguably it would be better to do this in a <VirtualHost> directive in the main httpd-vhosts.conf file - but since that's not an option:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^arabme.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.arabme.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^chiname.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(landing|marketing)
RewriteRule ^(.*)$ https://www.greatwall.com/$1 [R=301,L]
Assuming your .htaccess file is at the document root, you just had a couple of errors:
1: you don't need to repeat RewriteEngine On
2: you'd missed a / after greatwall.com in the RewriteRule which meant the redirect would go to https://www.greatwall.comwhatever rather than https://www.greatwall.com/whatever
I tested this on htaccess.madewithlove.be so it should work.

how to redirect to a subfolder from root, rewrite subfolder link to look like root, and then add some exceptions in htaccess?

I beg you pardon if this is a duplicate question. What I've found on the net didn't satisfy me at all.
As I asked in this question, I successfully rewrote the /public folder to the root url. So If I visit localhost or localhost/public apache redirects me to /public and hides public in the url. To sum up, I did it in this way:
RewriteEngine on
RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
However in this way everything will be redirected to the subfolder /public. I need to preserve, or to add some exceptions, to some subdirectories that are not in the /public folder.
+---assets
+---images
\---jp-pattern.jpg
+---javascripts
+---stylesheets
+---template
+---public
\---products
I need to preserve assets, images, javascripts, stylesheets, and template folders. I need them to be for example localhost/images and so on.
I tried with the images folder first in this way:
RewriteCond %{REQUEST_URI} !^/images [NC]
or
RewriteCond %{REQUEST_URI} !^/images/?$ [NC]
or
RewriteCond %{REQUEST_URI} !/images [NC]
but none of them worked. The browser always returns me this message:
Not Found
The requested URL /public/images/jp-pattern.jpg was not found on this server.
This means that I couldn't add the exception.
How can I add those exceptions?
You can add this block directly after RewriteEngine on:
RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets|template)/ [NC]
RewriteRule ^ - [L]
This says that if the request begins with either of those directories, don't rewrite. Use of the L flag makes that the last rule to be checked.
However, I advise that you actually do an existence check instead. Your HTML would likely be requesting an existing file, and so it would skip the public check anyway.
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]

Redirect sub domain to sub directory

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

.htaccess complex and tricky redirection

I am not expert in .htaccess that my need bit complex and tricky help..!
Here is my current .htaccess and remember i need some .htaccess that can destroy my current .htaccess.
Current .htaccess:
RewriteRule ^join$ https://example.com/internalpath/join/index.php [L,QSA]
RewriteRule ^join/(.*)$ https://example.com/internalpath/join/$1 [L,QSA]
Now my problem:
I want to redirect each and every file and directories to HTTP, except these two:
https://example.com/join/
https://example.com/join
Remember, I want every link on http except this above provided link, other then all files and directories and even files and directories in /join/ or /join will be HTTP, just two links on HTTP, but please it will not destroy the previous written .htaccess.
Try this (not tested):
RewriteCond %{REQUEST_URI} !/join/?$ [NC]
RewriteRule ^(.*)$ http://domain.com/internalpath/$1 [R=301,QSA,L]
You need these 3 rules:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(join/.+)$ https://%{HTTP_HOST}/internalpath/$1 [R=301,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(join)/?$ https://%{HTTP_HOST}/internalpath/$1/index.php [R=301,NC,L]
RewriteCond %{HTTPS} on
RewriteRule !^join http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

Wildcard Sub Domain Path Issues

I have an htaccess rewrite for wildcard sub domains which rewrites foo.domain.com to domain.com/index.php?id=foo. This is done using:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? index.php?id=%3 [L]
This works OK, but all content on the site is referenced to the root e.g:
"/content/imgs/logo.jpg" or "/ajax/upload.php"
The wild-card sub domain changes the root and all content is referenced to:
"http://foo.domain.com/content/imgs/logo.jpg"
And the content cannot be found because it is not located on this subdomain.
I know you can use the html < base > tags to place a prefix on all href locations but this does not help in javascript for ajax requests.
Is there anything that can be done in htaccess to solve this problem?
Thank you.
I think I may have the solution to your problem Christopher!
Your rewrite condition was being applied to ALL included content such ass css/js/images etc. Which mean it was trying to rewrite this content to index.php?id=%3 instead of style.css.
By adding the line RewriteCond %{REQUEST_FILENAME} !-f above the condition and placing the entire condition at the bottom of the htaccess means that it will only apply to files/directories that DO NOT EXIST (i.e. only to wildcard subdomains).
Try the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)$ "http://domain.com/index.php?id=%3" [L,P]
Hope this helps you mate!
W.
Ok, one problem right away, you aren't specifying a target domain on your rewrite rule. If all requests from any subdomain except www need to be redirected, you should specify that like:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? http://www.domain.com/index.php?id=%3 [L]
The other problem with this is that you are ignoring the file path after foo.domain.com, so that ALL requests get redirected to domain/index.php?id=foo regardless if the requested file was a page or an image.
A better way might be to give each "subdomain" its own folder, something like
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)? http://www.domain.com/%3/$1 [L]
so a path like foo.domain.com/content/imgs/logo.jpg would point to domain.com/foo/content/imgs/logo.jpg

Categories