Htaccess remove string from URL behind last slash - apache

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]

Related

URL rewriting and double slash after domain : how to make it single slash?

I want to write a rule to redirect (301) all pages from https://dev.example.com to https://www.example.com
On the root of dev.domain.com, on my .htaccess I have put this single line :
RewriteRule ^/(.*)$ https://www.example.com/$1 [R=301,L]
The redirection works but all subpages have a double slash.
Example : https://dev.example.com/contact.html is redirected to https://www.example.com//contact.html
Which is not working !
How can I solve this issue ?
Could you please try following, written and tested with your shown samples. Please clear your browser cache before testing your URLs. Also these Rules will look for URLs which are not having www in their HTTP_HOST variable part and then will remove domain name(very first part till first occurrence of .) with www and rest of the domain name's part.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?!www\.)(?:[^.]*\.)(.*)
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [NE,L]

htaccess redirect double slash URL 443

I have a webpage that's reachable via HTTPS and I have some Urls with a double-slash (//) included. Those I want to replace with single-slash (/) or just rewrite/redirect to another page.
https://www.domain.com/us//whatever/page/
https://www.domain.com/us/whatever/page/
Normally I do redirects like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^us/xyz/?$ https://www.domain.com/us/newpage/? [NC,L,R=301]
And double-slashes (//) in the RewriteRule don't work for for the htaccess redirects :/
I've already searched for a solution for replacing // with / and I found:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
But when I use this snippet and I open the URL with the double-slash - this happens:
Opened url: https://www.domain.com/us//whatever/page/
Redirects to: http://www.domain.com:443/us/whatever/page/
Need help here. How can I fix this?
And is there an additional way to rewrite just a specific url that includes a double-slash (//)?
Thank you very much!

Htaccess 301 change old URL

I would like change url in my htaccess and using redirect 301 for SEO.
my old url:
RewriteRule ^my-oldurl-(([0-9]+)-[a-zA-z0-9_-]+).html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L]
my new url:
RewriteRule ^my-newurl-(([0-9]+)-[a-zA-z0-9_-]+).html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L]
My last test (error 500):
RewriteRule ^my-oldurl-(([0-9]+)-[a-zA-z0-9_-]+).html ^my-newurl-(([0-9]+)-[a-zA-z0-9_-]+).html [QSA,R=301]
Thank you for any advice.
Your question is a bit fuzzy, but I assume this is what you are looking for:
RewriteRule ^my-oldurl-([0-9]+)-([a-zA-z0-9_-]+)\.html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L,R=301]
Or, if you prefer a 2 step approach you can do this:
RewriteRule ^my-oldurl-([0-9]+)-([a-zA-z0-9_-]+)\.html$ my-newurl-$1-$2.html [QSA,L,R=301]
and rely on the already existing rewrite rule for the new urls.
Also note the backslash escaping (\) I added to the dot (.) in the regular expression. It works without, but this version is more precise.

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]

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]