how can i remove trailing slashes form the url - .htaccess

I am having an issue with the trailing slashes at the end of the URL. I check the URL redirection online and the result shows me that I am having too many redirects on my page so I need to reduce those redirects I am having the following result of my link
https://example.com/xyz/pqr
302 Found
https://example.com/xyz/pqr/
302 Found
https://example.com/xyz/pqr//
302 Found
https://example.com/xyz/pqr///
302 Found
https://example.com/xyz/pqr////
302 Found
https://example.com/xyz/pqr/////
302 Found
https://example.com/xyz/pqr//////
302 Found
https://example.com/xyz/pqr///////
302 Found
https://example.com/xyz/pqr////////
302 Found
https://example.com/xyz/pqr/////////
302 Found
https://example.com/xyz/pqr//////////
302 Found
https://example.com/xyz/pqr///////////
302 Found
https://example.com/xyz/pqr////////////
302 Found
https://example.com/xyz/pqr/////////////
302 Found
https://example.com/xyz/pqr//////////////
302 Found
https://example.com/xyz/pqr///////////////
302 Found
https://example.com/xyz/pqr////////////////
302 Found
https://example.com/xyz/pqr/////////////////
302 Found
https://example.com/xyz/pqr//////////////////
302 Found
https://example.com/xyz/pqr///////////////////
302 Found
how can i resolve this issue?? i am having no idea where i am going wrong..!! Any help would be aprpeciated. Thanks in advance.
Here is my htaccess code :
RewriteEngine on
RewriteCond %{QUERY_STRING} p=3842
RewriteRule ^ /? [R=301,L]
That's all what i have in my htaccess code and the rest is just a 301 redirects and nothing more..!!

To remove a trailing slash using .htaccess you can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

Related

.htaccess redirect with space %20 not working

I have a link like this:
http://www.expamle.com/folder1/folder2/folder%20/file.html
I want to 301 redirect it with .htaccess to:
http://www.expamle.com/folder1/folder2/folder/file.html
I tried this:
RewriteCond %{REQUEST_URI} ^\/folder1\/folder2\/folder\%20\/file\.html$
RewriteRule .* http://www.example.com/folder1/folder2/folder/file.html [R=301,L]
but I'm getting:
Not Found
The requested URL /folder1/folder2/folder /file.html was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I also tried this
Redirect 301 /folder1/folder2/folder%20/Export1.htm http://www.example.com/folder1/folder2/folder/file.html
but got the same error.
What am I doing wrong?
The solution is to add quotes like this:
Redirect 301 "/folder1/folder2/folder /Export1.htm" http://www.example.com/folder1/folder2/folder/file.html

301 redirect with url parameters

To keep google ranking i need to relink for exemple
Redirect 301 /kataloger.asp?id=1 http://www.domain.se/kataloger/category/kopplingar
Redirect 301 /kataloger.asp?id=2 http://www.domain.se/kataloger/category/ventiler
and in others ignore all but the id
Redirect 301 /produkter.asp?id=15 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
Redirect 301 /produkter.asp?id=15&l=3 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
Redirect 301 /produkter.asp?id=15&l=4 http://www.domain.se/produkter/kopplingar/a-lok-kopplingar/
But they all relink to /kataloger/ and /produkter/ and additionally throw a 404 att google.
What do i do wrong?
Found the answer here: How to 301 redirect old urls with query strings
RewriteCond %{QUERY_STRING} id=1$
RewriteRule (kataloger.*) /kataloger/category/kopplingar? [R=301,L]
RewriteCond %{QUERY_STRING} id=15&(.+)$
RewriteRule (produkter.*) /produkter/kopplingar/a-lok-kopplingar/? [R=301,L]

htaccess: how to redirect all URLs with a percent sign after a slash to 404?

I already tried some Rewrite Rules but none of them worked out:
RewriteRule ^example/%(.*)$ - [R=404,L,NC,B]
RewriteRule ^example/\%(.*)$ - [R=404,L,NC,B]
RewriteRule ^example/%(.*)$ - [R=404,L,NC]
What is the correct syntax for that purpose?
Edit:
It's not redirecting to my 404 page, instead it gives me an Error 400. How could that be?

redirecting page to 404

I've found a few pages for redirecting a single page to 404 but it's not working. Here is my simple line
RedirectMatch 404 ^/showthread.php?p=3164554$
It does not work though. Am I missing something? Thanks!
Your configuration tries to redirect the page "404" to the page "^/showthread.php?p=3164554$" (see the documentation).
RedirectMatch generates a redirection : you can't redirect with a HTTP 404 code. When redirecting, you may have issues with the query string (I couldn't match the query string), I would use rewrite rules :
You can redirect to a 404.html page with
RewriteEngine On
RewriteCond %{QUERY_STRING} p=3164554
# the empty question mark discard the query string
RewriteRule ^/showthread\.php 404.html? [L,R=301]
Or you can stay on the same url but show the 404 page :
RewriteEngine On
RewriteCond %{QUERY_STRING} p=3164554
RewriteRule ^/showthread\.php - [L,R=404]
Try this way to escape your . character with \ like this.Let me know, it works for you or not.
RedirectMatch 404 ^/showthread\.php?p=3164554$

302 redirects being cached in firefox

So I have the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^https?://www.google.com [NC]
RewriteRule ^dir/file.html http://domain.tld/dir/index.html [L,R=302]
When I visit my site by clicking on it in the Google SERP it showsme dir/file.html. If I hit F5 to refresh it does the redirect. But then if I try to go to http://domain.tld/dir/file.html from dir/index.html I can't. I get 302 redirected, once again, to the index.
It's as though the browser is caching the 302 redirect when the only redirects it should be capturing are 301 redirects.
Any ideas?

Resources