Convert .html to -html in file name - wordpress

I converted my website to WordPress. In the process, in order to satisfy WordPress's rules about permalink formats, I had to change the filenames from ending in .html to ending in -html.
Unfortunately, there are backlinks from other sites that point to the old pages that end in .html. So when someone clicks one of those backlinks, the page cannot be found on the new site.
For this reason, I want to permanently redirect all requests for URIs ending in .html using .htaccess so that they end instead with -html.
So for instance:
https://example.com/file1.html
needs to permanently redirect to
https://example.com/file1-html
Sadly, my code generates 500 errors.
Here's what I wrote based upon what I found in the Apache manual:
<Directory /home/accountname/public_html>
RewriteEngine on
RewriteBase /home/accountname/public_html
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1-html [R=301,L]
</Directory>
Can someone help?

<Directory> directive isn't allowed in .htaccess.
Place this rule in your main WP .htaccess:
RewriteEngine on
RewriteRule ^(.+?)\.html$ /$1-html [R=301,L,NE,NC]
Make sure this is first rule below RewriteEngine on line.

Related

Apache rewrite to map # based URL into proper URLs

Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$

Subdomain/subfolder redirect conflict

We have a bunch of sites that are subdomains. We are changing the structure of these sites so they are a single site instead of multiple WordPress installs. No problem. Many of the subdomains have authority so we want to add 301 redirects from these subdomains to the new corresponding page. However, the new page parent page URLs conflict with the old subdomain structure.
existing subdomain
sub1.example.com
redirect which is now a landing page.
example.com/sub1
There is a conflict because the subdomain folders need to stay in place to perform the redirects.
I am looking for a way to create a redirect in the .htaccess from the subdomains that does not conflict with the new page URL of the same name?
it is preferred to leave the folders in tact so we can add 301 redirects to the .htaccess files in these folders
This is part of the problem. Any .htaccess file in these subdirectories will override the .htaccess file in the parent directory (the main WP directives I assume). (Strictly speaking it's just the mod_rewrite directives that are overridden. Other modules are inherited.)
Instead, try the following:
(NB: This assumes the subdomain points to a corresponding subdirectory off the main domain's document root. It would simpler if you changed the subdomain, so that it also pointed to the document root of the main site. You would then only need a single (simpler) redirect.)
Delete the .htaccess (and index.php) file(s) in the subdirectory and instead add the appropriate directives in the parent .htaccess file.
Near the top of the root .htaccess file (before any existing WP directives):
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub1|sub2|sub3)\.example\.com
RewriteRule ^[^/]+/(.*) http://example.com/%1/$1 [R=302,L]
This would redirect sub1.example.com/<anything> to http://example.com/sub1/<anything>. The www sub-subdomain on the initial request is optional.
Exactly how you would do this could depend on how many (and what format) subdomains you have. If you have just a handful (you say you have 3) then it would preferable to name these explicitly, as in the above RewriteCond directive.
It is easier to use a 302 (temporary) redirect whilst testing since they aren't cached by the browser. (Ensure the browser cache is cleared before testing.)
You will need to update the main WordPress directives to include an additional rule/condition for when these bare subdirectories are accessed (eg. http://example.com/sub1/). Since /sub1 is a physical directory, WP will not route the request, because the WordPress front-controller specifically ignores existing files and directories.
In the main WP .htaccess file, you have the following directives:
# 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
Add the following rule immediately after the above:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(sub1|sub2|sub3)/?$ /index.php [L]
This allows requests for the bare subdirectory (eg. http://example.com/sub1/) to be routed by WordPress.

redirect a domain extension to a subdirectory

I am trying to create a permanent htaccess redirect (301) from all my domain extensions into the appropriate subdirectories. The "rules" are as follow:
Redirect belgian website to its subdirectory on the main website:
from: www.example.be
to: www.example.com/befr/
Of course I would like to preserve the url parameters (if any) of the "from". Globaly, if someone entered the first url it should redirect to the second url (langage subdirectory in the main website).
I'm using wordpress and I'm hosting on a plesk I've read many things here but I'm stuck, thank you very much in advance for your help
PS: I've tried that but it doesn't work
RewriteCond %{HTTP_HOST} ^(www\.)?example.be$ [NC]
RewriteRule ^(.*) http://www.example.com/befr/$1 [L,R]
After reading your question, your code should be working (with informations you gave).
If it's not, here are some points to check:
1. Make sure mod_rewrite is enabled and htaccess can be executed (Apache config).
2. Your htaccess has to be in root folder (where example.be is forwarded).
3. About your htaccess' code:
since you're using Wordpress, make sure your rule is on top of other rules
don't forget to escape . (second one) in RewriteCond (otherwise it doesn't mean the same) even if it works that way
replace R flag (302 by default) by R=301 if you want a 301 redirect
Your code now looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.be$ [NC]
RewriteRule ^(.*)$ http://www.example.com/befr/$1 [R=301,L]
# your other rules (and Wordpress' default rule) here

Using .htaccess to redirect a domain to another URL

I have a site running wordpress, it's the full site. One of the pages is like a contact-us form located at www.ourdomain.com/contact-us/
I also have a URL like contactourdomain.com and I want it to redirect to www.ourdomain.com/contact-us/
We used to do this with a redirect on network solutions, but I prefer to have it all done right on the server if possible. I've got it sort of working but when you visit the link is still says contactourdomain.com/contact-us/ as the URL, and that breaks all the other ones.
Any suggestions?
.htaccess
Options -MultiViews
RewriteEngine on
RewriteBase /
# Rewrite
RewriteRule ^(.*)$ http://www.ourdomain.com/contact-us//$1 [L]
if you add this in .htaccess is it working?:
RewriteRule ^$ contact-us/ [R=301,L]
keep me posted..

htaccess url rewriting question

I have a site made with CodeIgniter with a WordPress site at /blog.
Say I create a page in WordPress that can be viewed at /blog/my-page.
Is it possible to rewrite the URL with .htaccess to remove the blog part of the URL? So I could enter my site url /my-page to view the page?
from the top of my head..
#start the engine
RewriteEngine on
#requests to www.yourpage.com/anything/
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$ [NC]
#are sent to www.yourpage.com/blog/anything/
RewriteRule .* /blog/%1 [L]
The rule below will rewrite (internal redirect) /my-page to /blog/my-page:
RewriteEngine On
RewriteBase /
RewriteRule ^my-page$ /blog/my-page [NC,L]
RewriteRule ^another-page$ /blog/another-page [NC,L]
This needs to be placed in .htaccess in website root folder.
If you already have some rewrite rules there then this one need to be placed in appropriate place as order of rules matters.
You still may need configure WordPress a bit so it understands that this URL is for him to process (WordPress may still see the original URL). I have not worked with WordPress that much to tell if this will be required (and how to do it if it is) -- but look at Permalinks settings.

Resources