As part of a migration I need to redirect sections of a site.
The below RedirectMatch is working, but the query string is being carried across.
I have tried using a "?" in various ways to remove the original query string, but have been unable to get it to work.
Current RedirectMatch:
RedirectMatch 301 ^/store/match.*$ http://shop.domain.com/new-directory/
The above RedirectMatch turns this:
domain.com/store/match-something-something-c-536.html?osCsid=123456…
Into this:
shop.domain.com/new-directory/?cPath=536&osCsid=123456...
But I want:
shop.domain.com/new-directory/
You need to use mod_rewrite for this:
RewriteEngine On
RewriteRule ^store/match http://shop.domain.com/new-directory/? [R=301,L,NC]
Related
Say I have a web folder with:
/index.html
/assets/css/styles.css
/assets/images/logo.png
/something.html
/holdingpage.html
/finale.html
/folder/somefile.html
/else.html
/PDFs/something.pdf
I want to put in place an htaccess RedirectMatch for this to "catch" all the files that are html files except the finale.html and redirect to there.
What I have so far is:
RedirectMatch 302 ".*/$" "/finale.html"
Which correctly catches all folder without filenames (indexes).
What I need is to catch all the html but not that destination one.
RedirectMatch 302 ".*(\.html)$" "/finale.html" # This loops obviously.
So I tried to be more specific;
RedirectMatch 302 ".*!(finale.html)$" "/finale.html"
RedirectMatch 302 ".*!(finale\.html)$" "/finale.html"
But this doesn't catch other files. Also by the looks of it I'd expect this to also catch assets and images.
I am trying to find a redirectMatch that finds every .html page that is NOT finale.html and redirect it to that page.
IT should be a redirectMatch and NOT a rewriteCond / rewriteRule. The user needs to see they're being redirected.
I imagine I want something like:
RedirectMatch 302 ".*((\.html) BUT NOT (finale.html))$" "/finale.html"
I have read the Apache Documentation but this doesn't really elaborate on this concept very much.
You can use a negative lookahead to match all .html files except for /finale.html (in the document root).
For example:
RedirectMatch 302 (?!^/finale\.html$)^/.+\.html$ /finale.html
IT should be a redirectMatch and NOT a rewriteCond / rewriteRule. The user needs to see they're being redirected.
I'm not sure of the logic here. mod_rewrite can produce external redirects just the same as RedirectMatch and in fact, you should probably be using mod_rewrite here if you have existing mod_rewrite directives (primarily to avoid unexpected conflicts).
With mod_rewrite, you can do it similar to above with a negative lookahead. For example:
RewriteEngine On
RewriteRule (?!^finale\.html$)^.+\.html$ /finale.html [R=302,L]
Or, use an additional condition to make an exception for finale.html, instead of using a negative lookahead. For example:
RewriteCond %{REQUEST_URI} !^/finale\.html$
RewriteRule \.html$ /finale.html [R=302,L]
Which is arguably easier to read and possibly more efficient (the regex at least).
This matches all URLs that end in .html, except for /finale.html (exactly) and externally 302 redirects to /finale.html.
Aside:
RedirectMatch 302 ".*!(finale.html)$" "/finale.html"
The ! in the middle of the regex is a literal character here. The ! prefix-operator is a feature of mod_rewrite. (It is a prefix-operator, not part of the regex syntax itself.
I've some dynamic urls. If I took a url with a query parameter, it leads to 404 page. So I would like to do a redirection using htaccess. I tried many possible solutions, and none of them worked.
Url structure will be /jobs/job-***.html?something and which I need to redirect to /jobs/job-***.html
I tried something like this, but returned 500 error;
RewriteRule ^jobs/job-([0-9]+).html?$ jobs/job-$1 [NC, L]
Please help to solve this problem.
You may use this rule:
RewriteCond %{QUERY_STRING} .
RewriteRule ^jobs/job-(\d+\.html?)$ %{REQUEST_URI}? [NC,L,R=301]
? after $1 in target will remove any query string.
You may use the following regex:
^\/jobs\/job-(\d+)\.html\?\S*$
(This essentially captures the job number)
and then replace it with:
/jobs-$1.html
Demo
RedirectPermanent /forum-old-f48.html /forum-neu-f1.html
301 Result:
forum-neu-f1.html?f=48&start=
Target: Dont append the "?f=48&start=" part. How can I make a stop behind the html?
The result should look like this:
forum-neu-f1.html
thank you!
To strip off query string you need to use mod_rewrite rule. Have this rule in root .htaccess:
RewriteEngine On
RewriteRule ^forum-old-f48\.html$ /forum-neu-f1.html? [L,NC,R=301]
You should test it after clearing your browser cache.
I have URLs that look like mysite.com/go/somevalue and I want to redirect them to mysite.com?shorturl=somevalue. Think of it as a branded url shortener.
I have tried the following among other incarnations. I think the problem is with the question mark in the results.
RedirectMatch 301 /go(.*) %3F$1
RedirectMatch 301 /go(.*) \?$1
RewriteRule ^.*/go/(.*)$ \?shorturl=$1
This RedirectMatch should work:
RedirectMatch 301 ^/go/(.+)$ /?$1
You don't want to escape the ? if you want tha tas part of the query string:
RewriteRule ^.*/go/(.*)$ ?shorturl=$1
The other two statements are mod_alias redirects, which will redirect the browser and cause the location bar to change. If that's what you want, you can add a [L,R=301] to the end of the rewrite rule and not have a need for the RedirectMatch lines
I add the code:
RedirectMatch 301 /san-pham/khuyen-mai/ /khuyen-mai
In default .htaccess file in opencart but the result will return:
http://myphamthiennhien.com/khuyen-mai?_route_=san-pham/khuyen-mai
You can test it here
http://myphamthiennhien.com/san-pham/khuyen-mai/
How can I solve it?
Use mod_rewrite to strip off query-string:
RewriteEngine On
RewriteRule ^san-pham/khuyen-mai/ /khuyen-mai? [L,R=301,NC]
Note ? in the end, that is required to strip query string.