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.
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.
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 have urls which include slugs that should be displayed in englich.
but the cms cannot interprete them right but can the german ones.
blah.com/en/team/sprache/french works but is not pretty
blah.com/en/team/language/french does not work, but should be in the browser address bar and is quite pretty.
I tried this .htaccess but it does not work - I land on a 404.
can anybody explain what goes wrong here:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# special rules for english urls
RewriteRule ^(.*)/language/(.*)$ $1/sprachen/$2
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
you need to know that the slugs can occure more then once in the url and there are some other slugs that must be translated as well.
Internal rewrite rules don't work with wordpress as WP uses index.php as default front controller for all the URLs.
You can use permalinks to customize how your URLs will appear to your clients.
For advanced URL customization use WP rewrite API to write your rewrite rules in PHP inside your theme. Here is a tutorial on WP rewrite rule APIs.
I relaunched a website that was originally created with Joomla, now using Wordpress for it (with subdomain multisite setup). The main domain remained the same.
Now I'm trying to redirect the old pages to the new ones via htaccess but it seems to cause a problem that the old URLs were formatted like "www.domain.com/index.php/de/oldpagename" because they seem to get stripped of index.php (and even the "/de/" oder "/en" parts) before the redirects are taking effect.
For example:
Redirect 301 /index.php/de/impressum http://de.domain.com/kontakt/
This leads to de.domain.com/impressum so the redirect is ignored but instead the URL is stripped of the index.php and there seems to be another redirect to the language subdomain based on the /de/ part.
I'm not very familiar with htaccess but my suspicion is that Wordpress' rewrite rules (and maybe a plugin - Multisite Language Switcher) are interfering here.
The only other code in the htaccess file is the Wordpress part:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
I tried moving the redirects above the Wordpress rewrite rules in the file and leaving out "/index.php" in the redirects with both making no difference at all. I also tried the Redirection plugin without success.
Does anyone have a solution for this?
I've been searching the web and Stockoverflow for hours now but everything that sounded promising didn't match my scenario or involved dynamic Joomla URLs.
Thank you very much in advance!
We have a WP install in the root of our server and its running great.. but, we just installed another app in a subdomain. Now, I can view the index.php of that app but cannot do anything with it.. the htaccess rules in the root (from WP base install) are effecting the requests.
So, how to I eliminate the WP htaccess file from effecting the subdomain?
Here is the htaccess contents for the root (WP install):
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN WordPress
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
And for the htaccess in the subdomain:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|stylesheets|js|images|user_guide|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I've search everywhere online and tried a couple samples I found.. nothing has worked.
Any help is greatly appreciated !
Thanks
UPDATE:
It seems that maybe Wordpress is not the culprit.. out of curiosity, I removed all lines in the WP .htaccess file.. and the app in the subdomain was still not working. Its rewrite rule must be wrong..
So, it is the second rewrite rule that is not working. If I type in /index.php?about then I can see the about page.. but it should display by going to: /about
WP is also the culprit (you have two problems).
I can fix the first .htaccess problem because WP is predictable. You want to put this before the BEGIN WordPress section:
RewriteRule ^subdir-name/.*$ - [PT]
That grabs any requests to your subdir and Passes it Through (PT) so that it is not hijacked by WordPress.
The problem in your second .htaccess is that it seems to be assuming it is still in the root directory. For this one, I can't be sure without seeing the layout of your app, but the / before index.php may well be wrong. Are you sure that the paths in the second file match the new layout of your files?
Um... should this work? For sub-directory "office" - cheers :-)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^office/.*$ - [PT]
# BEGIN WordPress
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>