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
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 have urls like this
https://www.example.co.uk/382-princeton-teak-6ft-garden-bench-chinoiserie-bench.html
That i want to redirect to
https://www.example.co.uk/princeton-teak-6ft-garden-bench-chinoiserie-bench.html
I tried with RedirectMatch 301 ^/(.+?)([0-9]+)-(.*)\.html$ /$1.html it do match but it redirects to https://www.example.co.uk/3.html any thoughts guys ?
You should be using this rule as number is right at the start:
RedirectMatch 301 ^/[0-9]+-(.+\.html)$ /$1
Clear your browser cache to test this change.
End of the url have numbers without any separator
Given url like
example.com/productdetails/product-flowers123
needed to redirect
example.com/productdetails/product-flowers--123
can i do this with .htaccess
I already tried: RedirectMatch 301 ^/productdetails/([A-Za-z]+)$([0-9]+)$ /productdetails/$1--$2
But its not worked.
You can use this rule instead:
RedirectMatch 301 ^/(productdetails/.*?[a-zA-Z])([0-9]+)/?$ /$1-$2
Make sure to use a new browser or clear your browser cache before testing this change.
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]
I need to redirect the following URL (and NOTHING with additional URL paths after the wholesale) :
www.jedzebel.com/wholesale (or jedzebel.com/wholesale)
to the following :
www.jedzebel.com/wholesale-info/wholesale-information.html
Unfortunately, I have a folder called wholesale already and when a standard 301 redirect is in place the mod_rewrite rules are causing the all URL's with wholesale in the path to rewrite incorrectly.
I tried the following in both RedirectMatch and Redirect in .htaccess after the Joomla rewrite rules but I must have something wrong :
RedirectMatch (.*)/wholesale?$ http://www.jedzebel.com/wholesale-info/wholesale-information.html [R=301,L]
Redirect 301 ^wholesale?$ http://www.jedzebel.com/wholesale-info/wholesale-information.html
Any ideas ? I need to ONLY rewrite exactly www.jedzebel.com/wholesale ...
You can make trailing slash optional in your rule.
You can try this rule:
RedirectMatch 301 ^/wholesale/?$ /wholesale-info/wholesale-information.html
Alternative:
RewriteEngine On
RewriteRule ^wholesale/?$ /wholesale-info/wholesale-information.html [L,R=301,NC]