I'm working on a project made by a software engineer and by a team of non-technical people who know how to use Wordpress. That means that part of the platform is handmade, but still need to have Wordpress contents to be handled by non-software engineers.
My idea is to have two folders in my webserver root, one called /app/ containing the handmade code, and one called /wp/. So when the GitHub pipeline release new code into /app/ is sure not to touch stuff in /wp/ containing Wordpress.
I have achieved forcing the HTTPS with the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It works like a charm, users are successfully redirected if they use HTTP.
Now the problem is, I want to "reserve" the routes used by the handmade platform for myself, and in case the user is not calling any of those routes, then I pass the ball to Wordpress. I want it to appear as it is a single website, so I don't want the user to load the /app/appRoute or the /wp/wpRoute, I'd like to always load /route1, /route2, without specifying the subfolder into the URL.
The handmade platform should have priority, and it uses around 13 main routes (and some of them have subroutes), so I can hardcode them into the .htaccess file I guess. If the user is trying to load any of those routes, then I want to load the content into /app/route, if not, I'd load /wp/route. Of course Wordpress has its own .htaccess file, and the platform has its own, which is:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I'm very bad at working on .htaccess files and been searching around, it looks like I can't find the solution for my scenario.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
Should I have two .htaccess, write the 13 routes first, and then eventually redirect to the WP .htaccess (is it even possible?)?
Do I risk to make the user face a "Too many redirects" error?
This hybrid solution confuses me a lot. Does anyone who has been in the same situation has suggestions? Thank you in advance.
Given the following requirements:
/app subdirectory contains the "handmade code"
/wp subdirectory contains the WordPress site.
Neither /app or /wp should appear in the visible URL.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
You could, but I wouldn't. Keep the WordPress .htaccess file in the /wp subdirectory. Everything WordPress is in the /wp subdirectory.
I would use 3 .htaccess files:
One in the document root. This manages the routing to either the "handmade code" in /app or /wp (WordPress). This should also manage the canonical redirects (ie. HTTP to HTTPS and www vs non-www)
One in the /app subdirectory that manages the routing within your "handmade code".
One in the /wp subdirectory that manages the routing within WordPress.
This allows you to keep the "handmade code" and WordPress entirely separate (in terms of development).
Your 3 .htaccess files would then look like this:
/.htaccess
# /.htaccess
RewriteEngine On
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite specific URLs to "/app" (handmade code)
RewriteRule ^app-route-1$ app/$0 [L]
RewriteRule ^app-route-2$ app/$0 [L]
etc.
# Rewrite everything else to WordPress
RewriteRule (.*) wp/$1 [L]
The "specific rewrites to /app" can be combined if there is a pattern. See below regarding static assets.
/app/.htaccess
# /app/.htaccess
RewriteEngine On
# Redirect any direct requests to "/app" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
/wp/.htaccess
# /wp/.htaccess
# Redirect any direct requests to "/wp" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Note the RewriteBase directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify /app or /wp in the .htaccess file itself. (Although this might mess with WordPress, that likes to (unnecessarily) use RewriteBase and will try to overwrite the WP code block.)
You do not need to repeat the RewriteEngine directive, that already occurs later in the WP code block.
I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within /app, ie. By including the /app path segment in the asset link. eg. <image src="/app/assets/images/myimage.png">. With WordPress you could link directly (ie. include /wp prefix) or omit /wp, since everything else is rewritten to /wp anyway.
Ideally, it would probably be preferable to omit both /app and /wp from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.
If your "handmade code" uses /assets for all the assets then you can rewrite these in the parent .htaccess file in the root, before your custom route rewrites:
# Rewrite "/app" assets
RewriteRule ^(assets)(?:/(.*)|$) app/$1/$2 [L]
This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.
Related
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).
I have WordPress installation with a subdirectory has the same name as the page. I rename this directory and I want to redirect to it whenever a specific URL has been requested.
In my wordperss I have >>> /resources path and I have
/resources/page1
/resources/page2 etc
I also have a subdirectory called resources and have subdirectories called page1 and has a file1.zip file.
I need whenever the user asks for /resource/page1 to open it from WordPress and whenever the user asks for /resource/page1/file1.zip to download it from the directory. This subdirectory can have multi subdirectories, eg. /resources/page1/releases/v1/file1.zip
I am fine with renaming the subdirectory as long as I can handle the redirection.
I tried these rewriting conditions but unfortunately, I don't understand them properly.
In the .htaccess of the main WordPress I added
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^resources/([^/]+/.+)$ /resources1/$1 [R,L]
</IfModule>
but this rewriting it's not working
The WP default routing will rewrite all requests that do not match a physically existing file or folder, ot its own index.php - so you need to do your additional rewrite before this WP stuff.
And you can not match just resources/(.+) here - because you have /resources/page1 as an existing WordPress page, and that one you don’t want to rewrite.
So demand at least one additional “folder” after resources/ first.
RewriteRule ^resources/([^/]+/.+)$ /resources1/$1 [R,L]
[^/]+ means, one or more characters out of the negated characters class [^/] first - that matches any character but a /.
By demanding an additional / after that, we have made sure, that we match at least one additional folder below resources/.
And then .+ matches one or more arbitrary characters again, including slashes, so that you can have foo.zip or bar/foo.zip or even bar/baz/foo.zip after that.
I want to change URLs in one folder (cartoon) in my site to friendly SEO URLs.
This folder (cartoon) includes a PHP script not related to WordPress.
From:
example.com/cartoon/index.php?v=TitleEpisode
To:
example.com/cartoon/TitleEpisode
I read here all related questions but I did not benefit.
I have WordPress on my main domain (example.com).
I found this code in .htaccess file:
DirectoryIndex index.html index.php index.htm parking-page.html
# 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
What do I do?
Ok, based on the edit, it seems like what you want is for WordPress not to rewrite that slug but to ignore it.
You can do this by editing your .htaccess to exclude a folder. Make sure the folder is in the root directory of your site, as in, the same folder as wp-admin, wp-content, and wp-includes.
Then, open your .htaccess and add a rewrite rule to ignore that folder:
DirectoryIndex index.html index.php index.htm parking-page.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# Add this condition with the folder you want to ignore (cartoon in your example)
RewriteCond %{REQUEST_URI} !^/(cartoon|cartoon/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
OLD ANSWER
Well, this is fairly open-ended. For a better answer, please edit the question to include what you have already tried. Also, what do you mean by "one folder"? Is this a custom post type? Is it a category or custom taxonomy? Are there different permalinks for different types/taxonomies? Please provide more details on what you want to do.
But for the usual case, here is the documentation for pretty permalinks.
First, make sure URL Rewriting is enabled in Apache. Example in Ubuntu/Debian
sudo a2enmod rewrite
sudo service apache2 restart
Now, in wp-admin, go to Settings -> Permalinks. Set the permalink to Post Name.
Then click save changes. It will either save the new .htaccess automatically if your site has the permissions to, or it will give you the new content of .htaccess to copy and paste.
Now you should be able to view a post or page and it should show the SEO-friendly links.
How are you implementing (or intending to implement) the routing of example.com/cartoon/TitleEpisode?
If this is entirely outside of WordPress then I would expect you to have an additional .htaccess file inside the /cartoon subdirectory (since this is presumably a physical subdirectory)? This alone should be sufficient to override the WordPress mod_rewrite directives in the parent .htaccess file, since mod_rewrite directives are not inherited by default.
For instance, simply enabling the RewriteEngine in a subdirectory is sufficient to override the WP directives.
In /cartoon/.htaccess:
RewriteEngine On
Then, in order to route a URL of the form /cartoon/TitleEpisode to /cartoon/index.php?v=TitleEpisode, you would need something like:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+) index.php?v=$1 [L]
(A bit similar to the WP directives in the parent .htaccess file.)
I would avoid editing between the # BEGIN and # END WordPress markers in the parent .htaccess file since these could be overridden by future WP updates.
You would instead implement an exception before the WP directives. For example:
RewriteRule ^cartoon - [L]
However, as mentioned above, you are probably better off creating an additional .htaccess file in the subdirectory and avoid touching the WordPress installation at all.
it's done.
i enter this after the first line in .htaccess
# BEGIN for Cartoon Folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?cartoon/(.+)$ /cartoon/?v=$1 [L,QSA]
# END for Cartoon Folder
thank u very much
WordPress: 3.5.1
How do I modify my .htaccess file to Rewrite/redirect old URL paths to the new paths after placing WordPress in its own sub-directory (using https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory -- Using a pre-existing subdirectory install )?
The Problem
I had an older Wordpress install in a folder called http://{www.mywebsite.com URL}/wp. I revised the website and followed the instructions for "moving" the site so now my domain uses simply http://{www.mywebsite.com URL}/ for the files. Wordpress runs everything now (unlike my old site where WP was just the bog link.) The moving instructions worked fine in general.
BUT, I want my old links (from search engines) such as http://{www.mywebsite.com URL}/wp/archives/1534 to redirect to http://{www.mywebsite.com URL}/archives/1534 . This does not occur with the default Rewrite Rules used by WordPress. In psuedo code:
Use the general rewrite rules unless
coming from a link where I want
to "strip" the /wp from the link.
I tried several rewrite variations citing the Apache mod-rewrite documents but none seem to work. The lines I tried in the sample .htaccess file below are prefaced by #Attempt {number}-- (and I realize that these are commented--in the real file they are uncommented.)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
#Attempt 1--
RewriteRule ^wp/(.*)$ /$1 [L,QSA]
# Default WP ruls
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
#Attempt 2 --
RewriteCond %{REQUEST_FILENAME} wp
RewriteRule (^/wp)(.*) $2 [L]
</IfModule>
# END WordPress
Is there a simple way to achieve this?
Try this -- it will do a full 301 redirect (which you want, since this is a permanent change to the URL):
RewriteRule ^wp/(.*)$ /$1 [R=301,L]
I'd also recommend putting it before the # BEGIN WordPress comment, since I think WordPress will rewrite what's inside there if you change certain settings.
I am trying to make my server look neat locally (I swear I am OCD) and what I am doing is combining a wordpress blog with all of my subdomains.
My wordpress blog is my main website, example.com. Whereas my subdomains are at sub.example.com.
Right now, I have this setup:
public_html
( wordpress files here )
subdomains
sub1 - (sub1.example.com)
sub2 - (sub2.example.com)
sub3 - (sub3.example.com)
blog - (empty)
The blog folder is empty. However, I want to move all the wordpress files to blog/ but keep the blog's URL at http://www.example.com/. Thus, http://www.example.com/index.php will go to http://www.example.com/blog/index.php, as well as all directories in blog/.
This code did not work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ blog/$1.php [L]
It instead gave me Index of /. Is there any possible way to do this?
The best way to do this would be to just move all the files to /blog and then redefine your Apache siteroot for the www. domain to include the blog folder like
DocumentRoot /(path)/public_html -> DocumentRoot /(path)/public_html/blog
That way you won't even have to use .htaccess rewrites to correctly map your blog folder to the root of the www domain.
If for some reason you can't do that, lets take a look at your htaccess rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
These 2 conflict. Without an [OR] at the end of your RewriteCond, the rules act as AND operator meaning each condition must be true. So you will never had a match that is both NOT a file (!-f) and IS a .php file (.php -f).
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ blog/$1.php [L]
Here you are appending .php to a match that already contains .php per the RewriteCond.
I would try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [NC,L]
If you move all files into /public_html/blog and there will be nothing in /public_html except your /blog and /subdomain folders, you really don't even need !-d & !-f since there would be no files or folders to match (except your subdomain folders but I'm not sure you want those being served up under www domain anyways). Seeing as how your wordpress css,js and images are now also going to be in /blog you would want their urls to be forwarded on too. Also you don't really need RewriteBase / when working in the root folder. Just start your target urls with a slash like /blog/$1. Finally I added in a condition to check that the target request doesn't already contain the /blog in order to prevent infinite redirects...
Don't forget that even with the redirect in there, you will still need to have your site address configured with /blog -
http://www.example.com/blog/
in the Wordpress settings.
Here is another working example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^$ /blog/$1 [R=303,L]