Mod rewrite / remove slash behind .html - apache

I need a mod rewrite 301 that remove a slash behind the .html from the url.
Example:
Good URL: http://www.example.org/some-stuff-t123.html
Bad URL: http://www.example.org/some-stuff-t123.html/abcdesftesttest
The Bad URL with a / after the html should rewrited (301) to the good URL.
I need a request URI that detect a /* after the html and if yes, rewrite it.
Thank you

Try:
RedirectMatch 301 ^/(.*)\.html/ /$1.html
or using mod_rewrite:
RewriteRule ^(.*)\.html/ /$1.html [L,R=301]
If you have other rewrite rules, make sure the one that redirects is before any rules you may already have.

Related

htaccess 301 redirect not redirecting

tried several ways to make redirect, but not successfull
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
or
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
and many others from internet, but all of them not working.
Need to redirect pages/portfolio to pages/portfolio/3 (for all languages - en, ru)
This is content of file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
RewriteCond $1 !^(pma)
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Since you are already using mod_rewrite (for an internal rewrite) you should also use mod_rewrite for this redirect, rather than using a mod_alias Redirect. Different modules execute at different times during the request, despite the apparent order of the directives in the config file.
However, your example is unclear. The first example includes a trailing slash; the second does not? Is there a trailing slash or not?
Try something like the following instead after the RewriteEngine directive:
RewriteRule ^(en|ru)/pages/portfolio$ /$1/pages/portfolio/3 [R,L]
This excludes the trailing slash. And assumes "all languages" are just en and ru. This is also a temporary (302) redirect. Change to a permanent (301) redirect (if that is the intention) only when you are sure it's working OK, since 301s are cached by the browser. You will need to clear your browser cache before testing.
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
:
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
Aside: Neither of these would have worked anyway. End flags like [END,R=301] are a mod_rewrite syntax, and do not relate to mod_alias (Redirect). And the URL-path matched by the Redirect directive should be a root-relative path beginning with a slash, not an absolute URL. See the Apache docs... https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect

Htaccess remove string from URL behind last slash

Looking for solution how to remove anything behind URL last slash.
example output URL:
http://example.com/pictures/123/category-name/
problems to avoid for this URL:
http://example.com/pictures/123/category-name/blah.html
http://example.com/pictures/123/category-name/blah/blah.html
http://example.com/pictures/123/category-name/blah/blah/blah.html
http://example.com/pictures/123/category-name/blah/
http://example.com/pictures/123/category-name/blah
So far i have this:
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/$ $1/$2/category-name/ [R]
RewriteRule ([A-Za-z0-9-]+)/([0-9]+)/category-name/(.*)$ category.php?VarA=$1&VarB=$2
it works for output URL and the URL with anything behind pattern. Problem is that it doesn't rewrite (redirect) to output URL if anything is behind last slash.
I'd personally use logic which is straight-forward like this:
#redirect to canonical url if no ending slash
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name$ $1/$2/category-name/ [R,L]
#redirect to canonical url if anything behind slash
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/(.+)$ $1/$2/category-name/ [R,L]
#rewrite to PHP file if visiting the canonical url
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/category-name/$ category.php?VarA=$1&VarB=$2 [L]

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

How to rewrite /?a=a to /a/?

How can I rewrite this URL:
http://mywebsite.com/?a=a
To this URL:
http://mywebsite.com/a/
With htaccess?
I tried this rewrite rule but it's not working:
RewriteRule /a /?a=a
Try this instead :
RewriteRule ^a/?$ /?a=a [L]
This will route http://mywebsite.com/a/ requests to http://mywebsite.com/?a=a
If you are only looking for an exact match of ?a=a in the URI you can use this.
RewriteRule ^\?a=a$ /a/ [R=301]
This assumes you want to rewrite the actual URL in the user's browser and provide a 301 header to let anything crawling the page know about the "proper" URI to use (thus the [R=301]).

htaccess URL Rewrite not working after 301 redirect

I have the following url rewrite:
RewriteRule ^info/([^/\.]+)/?$ info.php?page=info&subpage=$1 [L]
Originally the parameters were the page id's i.e. 0-10.
I have now changed this so the URLs have more meaningful slug names to reflect the content.
I have now set up the 301 redirects, for example:
Redirect 301 /info/0 http://www.example.com/info/intro
But the problem is, the redirect doesn't go to the url rewrite (http://www.example.com/info/intro). Instead it shows the full url (http://www.example.com/info.php?page=info&subpage=0)
How can it get it to keep the rewrite?
Many thanks
As posted by LazyOne, the solution was to use RewriteRule:
RewriteRule ^info/0$ http://www.example.com/info/intro [R=301,L]