htaccess 301 redirect wildcard - .htaccess

I am looking for a 301 wildcard redirect solution for the following.
I changed the following url layout.
https://www.example.com/print-scan
to
https://www.example.com/printing
Now all old pages should be redirecting to the new URL layout, so for example:
https://www.example.com/print-scan/scanners/product1
must go to
https://www.example.com/printing/scanners/product1
How can I achieve that?

You may use this rule as your very first rule:
RewriteEngine On
RewriteRule ^print-scan(/.*)?$ /printing$1 [L,NC,NE,R=301]
Pattern (/.*)? allows you match any of the following URIs:
/print-scan (no trailing slash)
/print-scan/ (with trailing slash)
/print-scan/anything
/print-scan/scanners/product1

Related

How to rewrite URLs that cause duplicate content?

I have a lot of links with a double URL structure. Basically, I need a .htaccess rule that redirects all URLs that start /de (for German language) to the same URL with only a /.
example.com/de/Shop/Tradition/Jagd-Forst/
to
example.com/Shop/Tradition/Jagd-Forst/
Your rewrite rule should match starting with /de/ (with an optional strating slash, capture everything afterwards with (.*) and redirect to the capture group ($1) with a 301 permanent redirect:
RewriteEngine on
RewriteRule ^/?de/(.*) /$1 [R=301,L]

htaccess 301 Redirect with Special Character Doesn't Work

I am trying to 301 redirect this URL https://example.com/#guest to https://example.com/forum
I tried this:
Redirect 301 /#guest /forum
Unfortunately It redirects to https://example.com/forum?
You may use this rule with RewriteRule that supports regex and precise matching:
RewriteEngine On
RewriteRule ^#guest /forum? [L,NC,R=301]
Test it after clearing browser cache again.
? in redirected URL will strip off any query string.

mod_alias redirects adding two trailing slashes

I have a number of URLs which may be entered with or without a trailing slash. They're used as vanity URLs.
.htaccess looks like this:
Redirect 301 /folder/vanity1 http://example.com/differentfolder/
Redirect 301 /folder/vanity2 http://example.com/a/third/folder/
Redirect 301 /vanity3 http://example.com/fourth/folder/
Users type in:
http://example.com/folder/vanity1
http://example.com/folder/vanity2
http://example.com/vanity3
http://example.com/folder/vanity1/
http://example.com/folder/vanity2/
http://example.com/vanity3/
When users type these in with no trailing slash, the redirects are working correctly, ending up at http://example.com/differentfolder/ with one trailing slash as desired.
The problem:
When users type these in with a trailing slash, such as http:/example.com/folder/vanity1/ they redirect to a URL with two trailing slashes, such as http://example.com/differentfolder// which throws a 404 error.
I have tried:
Removing all other lines from .htaccess, except these three vanity URLs and the standard WordPress block required to keep the site functioning. The WordPress block appears at the very end of the file:
# 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 checked not only with my browser but with WebConfs HTTP Header Check to make sure I wasn't seeing a cached rule. Still the same, if you type a vanity URL with no trailing slash it works; if you type it with one trailing slash, it redirects to two trailing slashes which causes a 404.
Adding a trailing slash in my rules:
Redirect 301 /folder/vanity1/ http://example.com/differentfolder/
This works fine if the user types the trailing slash, but if they leave off the slash it does not redirect and 404s because the URL http://example.com/folder/vanity1/ does not actually exist.
Question:
Is there a different way to 301 redirect the vanity URLs to a final destination with only one trailing slash, no matter which way the visitor types in the vanity URL - with or without a trailing slash?
This works as documented in Redirect,
Additional path information beyond the matched URL-path will be appended to the target URL.
This means /folder/vanity1 works only as a prefix, and an additional slash or anything else in the requested URL will be appended to the target, e.g. http://example.com/folder/vanity1/hi/there would result in http://example.com/differentfolder//hi/there
If you want to match exactly these two URLs, /folder/vanity1 and /folder/vanity1/, you must use either RedirectMatch or mod_rewrite
With a RewriteRule this would look like
RewriteRule ^folder/vanity1/?$ /differentfolder/ [R,L]
This pattern matches both with or without a trailing slash because of /?, which denotes an optional slash. The other rules would look similar.
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
And with RedirectMatch, it would look almost the same
RedirectMatch ^/folder/vanity1/?$ /differentfolder/
Same disclaimer applies here, when it works as expected, you may set the status code to 301.

htaccess simple Redirect doesn't work with trailing slash

I find a lot of answers to this question (and I have read dozens of them), but they are all about more advanced stuff with patterns and such stuff.
I just need a very simple and basic redirect for static urls.
If I add a trailing slash to the url, the redirect doesn't work and I just can't figure out why.
Example:
RewriteEngine On
Redirect 301 /content https://www.example.com/site/content.html
Redirect 301 /content/ https://www.example.com/site/content.html
https://example.com/content does work, https://example.com/content/ redirects to https://example.com/site/
What is the problem here?
Don't mix mid_rewrite rules with Redirect (mod_alias). Use this rule as very first rule in your root .htaccess:
RewriteEngine On
RewriteRule ^content/?$ https://www.example.com/site/content.html [L,NC,R=302]

Remove trailing slash from URL/folder but do not redirect subfolders

I'm using mod_rewrite. The .htaccess contains this line:
# redirect old references
Redirect 301 /folder/ http://www.donain.de/folder
This works fine execpt for one thing: All requests to subfolders are redirected as well. For example: /folder/hello is redirected to /folder. How can I prevent this?
I know that I can determine the start of an expression using ^ in a .htaccess file. But I can't figure out to say "stop this rule there".
You need to use RedirectMatch for regex support:
RedirectMatch 301 ^/folder/?$ http://www.donain.de/folder

Resources