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]
Related
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.
This is what I have in my root folder, and when I go to my url, this is what is showing and not my website. It is a wordpress site, only structured a little different. (roots.io/composer)
This is the root folder: www.mysite.com (I know the site works, I tested it on another web hosting server.)
Inside the folder 'public_html' is the index.php file. And if I click it I go to the site, but it looks like this: /public_html
Only site directories are showing and I suspect I need a working .htaccess file.. Anyone?
/public_html
And so I have a hunch that I need the correct .htaccess file in order to find the correct folders and files. Or am I wrong?
Some other details:
In the browser I use a 'working-url': new.mysite.com because the main url is routed to another webhost until we get the new site up and running. But the files are inside the root folder. (Not mysite.com/new)
I've set the siteurl to: https://example.com/wp
And homeurl to: https://example.com
I've tried writing a .htaccess file like this, but getting the 500 internal error message:
#This is for subdirectory
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteCond %{REQUEST_URO} !public_html/
RewriteRule (.*) /public_html/$1 [L]
#This is must
# 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've tried a lot of things, but can't seem to find the connecting dots. If someone out there with a clear head and more experience that could look into this, I would highly appreciated it.
Kind regards,
Me
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
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.
Is that possible, if yes then how?
When I write anything.abc.com/?p=37 must redirect to abc.com/?p=37.
I want to do it in wordpress.
First, remove the write permissions from your .htaccess file because WordPress is notorious for overwriting them. In .htaccess in your public_html root directory, add the following lines of code. Before you do this though, change the site url in settings >> general.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]
You can read more on BlueHosts website: https://my.bluehost.com/cgi/help/347
In your Wordpress settings > general settings, just change the site address URL to whatever you want.