Remove trailing slash wordpress website URLs - wordpress

I have wordpress permalinks set up to /%postname% (note there is no trailing slash at the end).
However, wordpress is not redirecting any given page URL from the version with the trailing slash to the non trailing slash version. Any website header checking tool would return a '200' (OK) code for both versions (with and without the trailing slash). I am concerned this will have negative SEO consequences due to duplicate content. Therefore I want to have only one version of each page.
I am trying to modify the .htaccess file located on the wordpress directory so it removes the trailing slash at the end of any URL pretty much redirecting to the non trailing slash version of the URL.
I have Wordpress installed on a sub directory not on the root of my site i.e.
http://mywebsite.com/wordpress/
I tried the following code which I compiled by reading different questions posted on this website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
The problem then is that I cannot login to wordpress admin. When I enter my user name and password on mywebsite.com/wordpress/wp-login.php
it typically used to redirect and enter to mywebsite.com/wordpress/wp-admin/
But now since the trailing slash is being stripped away from /wp-admin/ by the code I added to .htaccess then I get an error and cannot login to Wordpress.
I kept reading over this forums and then tried the following by playing around with the code and now I can login to wordpress but now the trailing slash is not being removed from any given URL.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} ^/wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} ^/wp-admin$
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Can anyone recommend what else I could do?
Pretty much all I want if for page URLs not to have a trailing slash at the end of the URL with the exception of the wordpress login page (mywebsite.com/wordpress/wp-login.php) and mywebsite.com/wordpress/wp-admin/
Keeping in mind the Wordpress install is on mywebsite.com/wordpress/ and not at root level.
Thanks so much!

Try these rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^((?!wp-admin/).+?)/$ /$1 [NC,NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

Related

.htaccess lowercase rewrite is not working

Very new to Apache here and htaccess rules. Please assume I'm doing something fundamentally wrong here. This is currently trying to be achieved currently on MAMP pro.
I'm trying to force all URL's to redirect to a lowercase version if the URL has any uppercase letters in it.
The solutions I'm finding are not reaping any results. .htaccess markup is as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# PROBLEM HERE: apparently this works
CheckSpelling on
CheckCaseOnly on
# END PROBLEM
# 301 redirect with trailing slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And when it comes to adding this to the httpd.conf:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
the server then does nothing. I've attempted wrapping it in <Directory> and <Virtualhost> but to no avail.

Static redirection in .htaccess with rewriterule

I should make some url redirections (no variables to pass) in .htaccess but I don't want to let see the destination url. How can I make?
I've already tried to use Redirection instruction like this:
Redirect /en/folder1/url1 /en/folder2/url2
But in this way the destination url is visible. So I started to try to use RewriteRule like in this example:
RewriteRule ^/en/folder1/url1$ /en/folder2/url2
but I've always error page as result.
P.S. This code is inside a .htaccess file with the default Wordpress url rewrite code.
EDIT. This is the complete .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^en/folder1/url1$ en/folder2/url2
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to remove the leading slash from your Rule's pattern
RewriteEngine on
RewriteRule ^en/folder1/url1$ /en/folder2/url2 [L]

Wordpress directory conflict: Need advice on htaccess paths

I am moving a website to Wordpress, and using the Multiblog functionality. I'm not a htaccess expert by any means, but I do manage to use it regularly. So I'm reaching out here for some expert advice.
The website, www.blather.ie will use the same path for the sub-blogs as the old website. I need to keep legacy folders - like www.blather.ie/zeitgeist and some of the old content while publishing a Wordpress blog to the same path. I realised when I tried to this that 403 error would occur. So I found a workaround here and here.
Adding lines to my htaccess as described in those pages to my exist file didn't work. When I cleared out the htaccess file and started from scratch, I no longer got the 403 error - the content appeared, but without any themes/stylesheets. I checked the source, and saw that it was trying to pull the info from www.blather.ie/zeitgeist/wp-content... which doesn't exist - it needs to pull it from www.blather.ie/wp-content/...
Also, this 'fix' also started creating loops in the Admin back end. Not sure why.
So, here's my original htaccess file:
# BEGIN WordPress
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Here's the same htaccess with the recommended fix for the directory conflict problem, but which still causes the 403 error.
# BEGIN WordPress
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Fix 403 errors on existing directories; WordPress overrides.
RewriteCond %{REQUEST_URI} ^/(zeitgeist)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Here's a "clean" htaccess taken from the 1st recommended website - it cures the 403, but breaks paths, points to the wrong stylesheet location and causes redirect loops and 403s in the backend instead:
# 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]
# Fix 403 errors on existing directories; WordPress overrides.
RewriteCond %{REQUEST_URI} ^/(zeitgeist)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So - my question - where am I going wrong with paths, and causing these damn feedback loops?
Thanks in advance!
Try this? Use the basic WP .htaccess, don't get fancy at all and instead drop a tiny .htaccess in each legacy folder (these are dirs right?) that disables rewrite in the folder.
This method worked for me as I needed to provide access to legacy folders as well. There are some options you may need to hit with casual variations for environments. There are a few ways to do this such as disable rewrite, allow dir access, show details, etc so I can't include a snippet for you at this juncture.

Wordpress messed up our htaccess redirect, how to modify to work with Wordpress' htaccess code?

We had several domains parked on top of our main website. This was a normal HTML website and we used the following redirect so that the domain would 301 redirect to the proper url to avoid getting dinged by Google.
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,L]
So the above htaccess code just rewrites the url to www.ourmainsiteurl.com, which is what we want.
Now here's the problem... we installed Wordpress and it has the following default htaccess code:
# 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 to add my redirect code in but that broke Wordpress. Can anyone tell me what I need to use to make this work?
Simply put I want all the parked domains (ie: parkedurl1.com, parkedurl2.com, etc) to be redirected to www.ourmainsiteurl.com with the htaccess file.
Make sure the redirect is before any wordpress rules:
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$ [NC]
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,L]
# 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
If it's still not working, check to see if wordpress has something turned on that is redirecting, or adding/removing the "www" from the hostname.

Remove trailing slash using .htaccess in WordPress

I'm looking for a way to remove trailing slash for all WordPress URL's.
I found similar answers like this one but it doesn't work when there's WordPress .htaccess rules before.
Here is my current WordPress .htaccess:
# 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
It might be as simple as go to settings > permalinks in the wp admin and remove the trailing slash at the input box for custom structure
Here are the steps to remove trailing slashes site-wide. If you simply wish to remove the trailing slashes on posts only, skip to Step 2.
Important Things To Consider
In .htaccess, the code between lines # BEGIN WordPress & # END WordPress may get reset by WordPress. Avoid changing code between those lines.
Forcing removal of trailing slashes, causes a loop in example.com/wp-admin. You can avoid the issue by excluding directories RewriteCond %{REQUEST_FILENAME} !-d.
The following solution addresses those issues.
Step 1 - Update /.htaccess file
Add the following code before the # BEGIN WordPress line in your /.htaccess file. This redirects URLs with trailing slashes to URLs with no trailing slashes.
# Remove trailing slashes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
</IfModule>
Step 2 - Update Permalinks
As others have pointed out, you must also update your Permalinks (Settings -> Permalinks) to Custom Structure, and remove the trailing slash there. It removes the trailing slash on all your posts.
Try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule (.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If it doesn't work try this one:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule (.+)/$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
To solve this trailing slash in WordPress URL problem, log in to your website admin panel -> go to Setting Tab -> click on Permalinks under Settings. It will open a page something like the below screen:
Now, check to verify the setting you have opted for the website URL structure. Make sure that you are not leaving a trailing slash in the URL structure.
https://blog.techblogsearch.com/2018/11/29/guide-to-remove-trailing-slash-from-wordpress-url

Resources