.htaccess redirect with questionmark - .htaccess

I want to redirect certain urls matching a pattern to the site root.
I'm doing:
RedirectMatch 301 ^/ABCDE.*$ /
But whenever there is a ? in the url the resulting link is whatever is after the url. How can I redirect also urls including questionmarks?

To strip off existing query string you need to use mod_rewrite rules. Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^ABCDE.*$ /? [L,R=301,NC]

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]

redirecting to a different directory using htaccess

I'm trying to redirect the following
/books/categories/mountain-literature/my-father-frank.html
to
/books/categories/baton-wicks/my-father-frank.html
This is the line in my .htaccess
RedirectMatch 301 /mountain-literature(.*) /books/categories/baton-wicks$1
it rewrites the url and appends this pageUrl stuff on which I don't want and the correct page doesn't load ?
books/categories/baton-wicks/my-father-frank.html?pageUrl=books/categories/mountain-literature/my-father-frank&contentType=html
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mountain-literature/(.*)$ /baton-wicks/$1 [L,R=301]
try this or this
Redirect 301 /mountain-literature /baton-wicks
If you dont need the rewrite rule to match multiple URL's, try just hardcoding the target URL, i.e.
Options +FollowSymLinks
RewriteEngine On
RewriteRule mountain-literature/(.*) /books/categories/baton-wicks/my-father-frank.html [L,R=301]
By adding the [L] flag it should tell apache not to allow any other rules to append to the URI

How to redirect urls starting with numbers

What will be a 301 redirect rule using htaccess to redirect urls similar to
domain.com/community/783-anystring-string/profile
to
domain.com/community/
or
any url matching this type of format
domain.com/community/123-anystring-string/...
to
domain.com/community/
Basically I want to redirect any urls domain.com/community/(starting with numbers)-string...
to domain.com/community/
You'll want to use mod_rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^community/[0-9]* /community [R=301,L]
I think. That may not work, I can't test it right now :/

Redirect URL with wildcard variables

I am trying to use the htaccess file to redirect a url with random url variables for example:
somesite/news/training-viewdoc.htm?file=somethingrandom
to
somesite-two/news/training-viewdoc.htm?file=that variable
Any clues as to how to do this. Right now I have....
RedirectMatch 301 /news/training-viewdoc.htm?file=(.*) somesite-two/news/training-viewdoc.htm?file=$1
You can't match against the query string in a RedirectMatch, however, it should automatically be appended so you don't need to worry about it:
Redirect 301 /news/training-viewdoc.htm somesite-two/news/training-viewdoc.htm
If you need to only redirect when there's a file in the query string, then you have to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^file=.
RewriteRule ^news/training-viewdoc.htm$ somesite-two/news/training-viewdoc.htm [L,R=301]

htaccess: how to redirect every url that starts with http://example.com/shop

Like the title says, how do I redirect every url that starts with http://example.com/shop to a single page?
so I have:
http://example.com/shop/index
http://example.com/shop/detail/52
http://example.com/shop/download
.... etc, and it should be redirected to http://example.com/new_page
You can use mod_alias’ RedirectMatch:
RedirectMatch ^/shop(/|$) /new_page
Or mod_rewrite:
RewriteEngine on
RewriteRule ^shop(/|$) /new_page [R]
Note the different pattern for RewriteRule as mod_rewrite removes the contextual path prefix in per-directory rewrites before testing the rules.

Resources