I have a wordpress installation in the root and another one in a subfolder within the root.
What would normally happen is that the url would look like:
https://example.com/quotes/us/some-url
but I wanted to remove 'quotes' from the url so it just ended up like:
https://example.com/us/some-url
Thanks to another stack overflow user, I was able to get that to work with the below htaccess code but I didn't realise that the images are now not showing and I get a 404 error for all of them. This is the root .htaccess file
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]
# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
and the subfolder 'quotes' .htaccess looks like this
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quotes/index.php [L]
</IfModule>
# END WordPress
I get a 404 error to this path https://example.com/wp-content/themes/theme_quotes/style.css?ver=1.0.0. So, it is looking in the root installation where that theme doesn't exist as it only exists in the subdirectory installation
Currently, we only rewrite requests to the /quotes subdirectory when the URL-path starts with a 2-letter language code, since that is the only thing that appears to differentiate the URLs between the two WordPress installs. However, that means that URLs to your static resources (as above) that do not have the language code prefix (and do not reference the /quotes subdirectory directly) are not being rewritten and so fail with a 404.
This could perhaps be fixed in WordPress, by including /quotes in the URL to your static resources. But that does expose the /quotes subdirectory for anybody looking at your HTML source. We would also need to modify the redirect directive in the /quotes/.htaccess file to prevent these requests being redirected back to root. EDIT: Actually, it looks like this is happening with your images already which already include the full ("correct") URL-path.
What we could do... in the root .htaccess file, rewrite any request for a static resource (image, CSS or JS file) to the /quotes subdirectory if it doesn't exist in the root. For example:
# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]
# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif)$ quotes%{REQUEST_URI} [L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
This does mean that should you have two static resources with the same name (same base URL-path) in both installations then the one in the root installation will "win".
Note that this is a "blind" rewrite... if a particular static resource does not exist in either installation then you will always get the 404 in the /quotes installation. But there's no way to really resolve that since there is an element of ambiguity in the URL-path structure.
AND, in the /quotes/.htaccess file, prevent any direct requests for static resources being redirected back to the root. For example:
# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|webp|gif)$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
I'm assuming all your file extensions (to static resources) are lowercase.
You will need to clear your browser cache, since the image redirect back to root will likely have been cached by the browser (since this is a 301 - permanent - redirect).
Related
At root there is WordPress site. And in subdirectory calles 'vue' there is vue build is there. In wordpress URL if 'podcaster' or 'crowd' is not a part of a URL then I want to load vue page without changing browser URL.
Below is my folder structure
Target is:
www.example.com/podcaster //display in the address bar, points to WP (var/www/html/)
www.example.com/crowd //display in the address bar, points to WP (var/www/html/)
www.example.com/username //display in the address bar, points to VUE (var/www/html/vue)
www.example.com //display in the address bar, points to WP (var/www/html/)
Below is the WordPress root directory .htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrite rule for vue
RewriteCond %{REQUEST_URI} !^/crowd/
RewriteCond %{REQUEST_URI} !^/podcaster/
RewriteRule (.+) /vue/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Below is vue sub directory .htaccess:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
You've not stated exactly what the problem is, however...
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteRule ^(.*)$ /vue/$1 [L,NC]
This will result in requests for the root being internally rewritten to the /vue subdirectory (I'm assuming you've placed this at the top of the WordPress .htaccess file in the root directory). You've stated that WordPress should be served from the root. In which case you should change the RewriteRule pattern from .* (0 or more) to .+ (1 or more) to avoid being triggered for requests to the root (base URL / homepage).
You will also need to ensure that rewritten requests (by the WordPress front-controller - to index.php - in the code that follows) are not also rewritten (otherwise everything will ultimately be rewritten to the /vue subdirectory). We can do this by adding another condition that checks against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to 200 (as in 200 OK status) after the later rewrite.
UPDATE: And I suspect you also have a number of static resources (CSS, JS, images, etc) that also need to be excluded, so we probably need to add a filesystem check for those, so the request is not rewritten if it already maps to a file (or directory).
For example, bringing the above points together, this becomes:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /vue/$1 [L]
I've removed the NC flag as that was superfluous here. Note also that the condition is successful when the string "/podcaster/" (note that slashes) does not appear anywhere in the URL-path. Should this not be restricted to the start of the URL-path perhaps? eg. !^/podcaster/.
Also, where is the rest of the WordPress .htaccess file? The WordPress front-controller (the part that normally appears after the # BEGIN WordPress comment marker) should appear after your custom directive. If you place your custom rewrite at the end of the WordPress .htaccess file, after the WP front-controller section then your directive will not doing anything since all requests will be routed to WordPress.
Note that you should place your custom directives before the # BEGIN WordPress comment marker. You should never edit the code between the # BEGIN WordPress and # END WordPress comments since this block of code is maintained by WordPress and your code could be overwritten when WP updates (unless you take additional steps to prevent this). There is no need to repeat the RewriteEngine or <IfModule> directives.
Below is vue .htaccess which in "vue" sub directory
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vue/
RewriteRule ^vue/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vue/index.html [L]
</ifModule>
The Vue .htaccess file isn't quite right (although should work OK). The first RewriteRule directive (which is simply an optimisation) should not include the vue subdirectory in the regex. In other words, it should be written:
RewriteRule ^index\.html$ - [L]
Since the URL-path that is matched is relative to the filesystem path that contains the .htaccess file.
In fact, you can remove all instances of vue from this file (which makes it simpler and more portable), providing you also remove the RewriteBase directive entirely. For example:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Relative substitution strings (ie. index.html in the 2nd RewriteRule directive above) are relative to the directory that contains the .htaccess file (providing you don't have a RewriteBase directive that states otherwise). So, the above naturally rewrites the request to /vue/index.html without having to explicitly state the directory.
I decided to change the domain name on my current website, and did that by changing the main domain and changing the settings in wordpress dashboard. I have both domains connected to the hosting (same on before changing the domain) and I used the .htaccess to redirect the old one to the new one.
Now it works, when I type the old domain, I get sent to the new one. However, my problem is this doesn't work for literally any other link. So if I try to access a post, it does not redirect me to the same page in the new domain.
I have searched everywhere, but I can't seem to solve this. I also use different redirection tools to determine if it works or not, I am not just going off my own network.
Example :
I type:
https://olddomain.com/blabla/bla-bla-bla/
I land on :
https://olddomain.com/blabla/bla-bla-bla/ instead of https://newdomain.com/blabla/bla-bla-bla/
The code I am using in my .htaccess file is:
# END LSCACHE
# BEGIN NON_LSCACHE
# END NON_LSCACHE
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "https\:\/\/newdomain\.com\/" [R=301,L] ```
Well you implement a redirection specifically only for the URL with trivial path "/". If you want to redirect all possible paths then you need to do so:
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [R=301,END]
You should place that redirection before the internal wordpress rules in case both set's are served from the same location, so configured by the same file. So place the higher up in that file.
I would recommend to start out with a temporary redirection (R=302) and only to change that to a permanent redirection (R=301) once you are sure everything works as expected.
I set up my web server having created a .htaccess file and subdirectory of 'www' in which I placed my Wordpress install.
Having run the install, everything appears to be redirecting as would be expected with the exception of the home page which throws a 404 error as the redirect rules contained in the .htaccess file are adding /www/ to the URL.
Guessing it's probably something simple, but need some objective help.
Obviously my actual domain has been substituted for 'domain.com'
# Turn off indexing of files and directories.
Options -Indexes
# Turn on rewrites.
RewriteEngine on
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Only apply to URLs that aren't already in the subdirectory.
RewriteCond %{REQUEST_URI} !^/www/
# Rewrite all those to insert the subdirectory name.
RewriteRule ^(.*)$ /www/$1
# Redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ www/ [L,R=301]
Turns out it's a simple fix just as I had first thought.
By replacing [L,R=301] in the final set of rules with [L] prevents the home page coming back to itself with the addition of the subdirectory.
I have a wordpress site eg. http://www.domain.com/ with custom theme hosted on a linux server and working fine. Some marketing strategy requires me to distinguish between 2 kinds of URLs (that they will be shooting from ads) and based on that redirect the site to different links.
eg:
If the URL contains /xyz/ ie:
http://www.domain.com/xyz/category/post I want it to be redirected to
an intermediate page ie: http://www.domain.com/intermediate.php and if
the url doesnt contain /xyz/ ie:
http://www.domain.com/category/post the post should show up as
usual.
I found out that this cant be done inside the wordpress code as before the first hook is triggered, the url is processed and a 404 page is thrown.
The only way I can achieve this is by modifying .htaccess file.
I found a code which reads:
RewriteRule ^(.*)/xyz/(.*)$ http://www.domain.com/intermediate.php [L,R=301]
and second way reads:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php
I am really confused about using it with the existing code in the htaccess file created by wordpress which reads:
# 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
I tried merging the codes ie:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /domain/index.php [L]
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule RewriteRule ^(.*)/xyz/(.*)$ http://domain.com/intermediate.php
</IfModule>
but only one of them work at a time ie: when the redirection works, wordpress posts doesnt show up and vice versa.
Kindly show me a better way.
Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this:
RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]
This will search for "xyz/" anywhere in the requested URL and redirect when found. Note that in .htaccess files, the directory prefix (/ in this case) is removed from the URL path before pattern matching. So, a pattern that starts / will never match at the start of the URL.
External redirects should generally come before internal rewrites, which is what the default WordPress directives do.
The alternative method you mention is less efficient:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]
This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. Note that the REQUEST_URI does start with a / (directory prefix).
I installed wordpress in a subdirectory, lets say www.example.com/wordpress. I want the user to hit www.example.com and see the index of www.example.com/wordpress. I'm already using a custom pretty permalinks structure /%category%/%pagename%/ so the htaccess file is a little foreign to me.
My current htaccess file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php
I've tried numerous solutions from browsing across the web but nothing seems to work. I either get internal 500 errors or view previous pages in the root directory like example.com/about (even though I've deleted the original about.html in the root) even after I deleted my cache.
Any help you could give would be extremely helpful. Thanks for your time,
Rob
The .htaccess file needs to be located in / and not in /wordpress/ and RewriteBase should be / not /wordpress/. Leave the permalinks setup on, but don't use an .htaccess file in /wordpress/. You'll need to set the root directory in your wordpress install to / and setup redirects for /wordpress/wp-content/ and the other wordpress subdirs in /.htaccess so that requests to those directories are also forwarded correctly.
I assume that you want to set up wordpress this way because there are other directories not associated with the wordpress install that you want to be accessible from / (www.example.com/cgi-bin/ for example). You'll need to set up your .htaccess file in / to make any directory aliases (like /cgi-bin/) accessible so requests to /cgi-bin/ don't get redirected to wordpress.
EDIT:
I haven't done something like this in a long time, but here's my best guess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/(.*)$ /wordpress/wp-content/$1 [L,QSA]
RewriteRule ^wp-admin/(.*)$ /wordpress/wp-admin/$1 [L,QSA]
RewriteRule ^wp-include/(.*)$ /wordpress/wp-include/$1 [L,QSA]
RewriteCond %{REQUEST_URI} !^/yourAliasUnrelatedToWordpress [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L,QSA]
</IfModule>