Remove trailing slash using .htaccess in WordPress - 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

Related

htaccess: Remove trailing slash from URL ending with .xml/ only

I need to remove trailing slash from URLs ending with .xml/ only ..
For this purpose I've created a Rewrite Condition and Rule which is working perfectly fine for the test link http://website.com/test.xml/
Test Link: http://htaccess.mwl.be?share=6fe08232-438a-53fa-8f1a-1f7f69b77b6f
The problem is when I place the rule in WordPress .htaccess file, it doesn't work at all! Seems like WordPress or YOAST Permalink structure is overriding the rule .. please help!
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} /(.*).xml/$
RewriteRule ^ /%1.xml [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Use the following code in your htacess file:
RewriteRule (.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} /(.*).xml/$
RewriteRule ^ /%1.xml [L]
If not working then download new wordpress and try in that.

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]

Remove trailing slash wordpress website URLs

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>

WordPress add slash in URL on 1 page

I have some problem with WordPress.
On the news page WordPress add / in the end of URL (example: mysite.com/news.html/).
On another pages all it's okay, (example, mysite.com/page.html).
Can anyone say me how delete / on the news page?
Maybe problem with .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Maybe problem in this line: prntscr.com/1yqevq but if I change for ptk-avtom.ru/%postname%. Without /, all pages are in 404 error.
Try to add this one in your .htaccess file:
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
Thanks

Forcing non-www url in Wordpress installation causes infinite loop

I am trying to rewrite http://www.domain to http://domain in a wordpress installation, but it looks like it's making the url loop from http://www to http:// and back to http://www, etc.
This is my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
What's wrong with this code?
WordPress can do this for you. Remove the .htaccess stuff you've added, and go to Options -> General and change your site's URL.
It looks like you made changes to your .htaccess file that aren't necessary. To fix this problem change your .htaccess file back to default wordpress value which is:
# 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
Now that your .htaccess file is back to normal, login to your wordpress dashboard and click on Setting -> General. You'll see two text boxes labeled "WordPress address (URL)" and "Site address (URL)". Change the URL in both of these fields and you can remove the www part if you don't want that in your site URL. Save the changes and you're good to go! Here's a screenshot of the settings page so you can see the exact fields:
Hope this answers your question!

Resources