Htaccess rewrite with question mark in condition - .htaccess

I want to redirect all
homepage.com/?start=
to
homepage.com
but not all other urls with ?start= such as
homepage.com/xxxx/?start=
i tried this:
RewriteCond %{QUERY_STRING} start [NC]
RewriteRule .* http://homepage.com? [R=301,L]
but sure, it redirect to home page all URLS with ?start

RewriteRule .* http://homepage.com? [R=301,L]
The RewriteRule pattern, (eg. .*) matches against the URL-path.
The regex .* matches everything, so that's why it redirects every URL-path. To match an empty URL-path (ie. just the home page) then restrict the regex to match an empty URL-path. eg. ^$.
RewriteCond %{QUERY_STRING} \bstart= [NC]
RewriteRule ^$ http://example.com/ [QSD,R=302,L]
Assuming you're on Apache 2.4 then use the QSD (Query String Discard) flag instead of appending an empty query string to the substitution string. Note that there should be a slash after the hostname portion of the URL.
Note that the regex start matched that string anywhere (eg. mystart123, starter=, etc.). The regex \bstart= matches just the query string parameter name as stated.
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

Related

Rewrite URL with query parameters in .htaccess

Say I have these urls:
https://example.com/bbs/board.php?bo_table=cad
https://example.com/bbs/board.php?bo_table=videos
https://example.com/bbs/board.php?bo_table=news
How can I rewrite these in .htaccess to something like this:
https://example.com/cad
https://example.com/videos
https://example.com/news
This is my attempt thus far. I know that my rewrite method is solid because it works on URL's without query strings. I tried the QSA flag (Query String Append) to no avail.
Options -MultiViews
RewriteRule ^bbs/board.php?bo_table=cad$ /caster-cad-downloads [R=301,L,QSA]
RewriteRule ^caster-cad-downloads$ bbs/board.php?bo_table=cad [END]
RewriteRule ^bbs/board.php?bo_table=video$ /caster-videos [R=301,L,QSA]
RewriteRule ^caster-videos$ bbs/board.php?bo_table=video [END]
RewriteRule ^bbs/board.php?bo_table=news$ /news [R=301,L,QSA]
RewriteRule ^news$ bbs/board.php?bo_table=news [END]
How can I rewrite to a different URL instead of the query string while still using the
%{QUERY_STRING method?
RewriteCond %{QUERY_STRING} ^bo_table=(cad|videos|news)$
RewriteRule ^bbs/board\.php$ /%1 [QSD,R=301,L]
# RewriteRule ^(caster-cad-downloads|caster-videos|news)$ bbs/board.php?bo_table=$1 [END]
RewriteRule ^(?:caster-(cad)-downloads|caster-(videos)|(news))$ bbs/board.php?bo_table=$1 [END]
How can I rewrite these in .htaccess to something like this:
The "rewrite" is the other way round (as mentioned previously). The incoming request is for /cad and this is internally rewritten to /bbs/board.php?bo_table=cad that actually handles the request.
This can be achieved with a single rule since these 3 URLs follow the same pattern (although that conflicts with the code sample you've posted). For example:
RewriteRule ^(cad|videos|news)$ bbs/board.php?bo_table=$1 [END]
The $1 backreference contains the value of the first capturing group in the RewriteRule pattern. ie. either cad, videos or news.
The external redirect is not strictly necessary, unless you are changing an existing URL structure. Note that the RewriteRule pattern matches against the URL-path only, which notably excludes the query string. (So your rules that include a query string would never match.) To match the query string you need an additional condition (RewriteCond directive) and match against the QUERY_STRING server variable. For example, the following would go before the above rewrite:
RewriteCond %{QUERY_STRING} ^bo_table=(cad|videos|news)$
RewriteRule ^bbs/board\.php$ /%1 [QSD,R=301,L]
Note that we need to use the QSD flag here in order to discard the original query string, we don't want to append it.
The %1 backreference (as opposed to $1) matches the capturing group in the last matched CondPattern (RewriteCond directive).
Don't forget to backslash-escape literal dots in the regex in order to negate their special meaning.
UPDATE:
RewriteRule ^(cad-downloads|cad-videos|news)$ bbs/board.php?bo_table=$1 [END]
To pass cad, videos (video?) or news as the URL parameter, you could do it like this:
RewriteRule ^(?:(cad)-downloads|cad-(videos)|(news))$ bbs/board.php?bo_table=$1 [END]
This is made possible because cad, videos and news are still part of the requested URL. The outer regex group is made non-capturing (with the ?: prefix). An additional capturing group inside this captures the necessary part of the requested URL.
However, the reverse is not possible without hardcoding the mappings.
I'll see if I can get back to your other queries/chat tomorrow...

htaccess redirect with query string not working

enter code hereI have a WordPress website.
I have URLs for affiliates that look like this:
https://example.com/folder/?ref=23432
https://example.com/folder/?ref=13442
etc.
I would like to redirect any URL that ends in ?ref= to another domain.
For example, https://example.com/folder/?ref= should redirect to https://example.org/product/
How can I do this? I appreciate your time.
I tried
Redirect 301 example.com/folder/?ref https://example.org/product/
Thank you #MrWhite. I tried the following with no success.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
The RewriteRule directive matches the URL-path only (less the slash prefix). So this should be matching against folder/ (as per your example), not the hostname.
And the $0 backreference in the substitution string is not required here. So this should simply be:
:
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
If you do need to check the requested hostname (ie. example.com) - if example.com and example.org point to the same server - then you need a separate condition (RewriteCond directive). For example, the complete rule would then become:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
Note that the regex (^|&)ref= matches the ref= URL parameter anywhere in the query string, if there happened to be other URL parameters that preceded it.
Reference:
htaccess redirect URL with parameter when a special parameter exists
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Redirect rule to remove query string from URL

Have been trying to write a redirect rule with query string but did not succeed.
I have the URL example.com/blog/?page=1 or example.com/blog/?hello, so it does not really matter what goes in the query string. How do I write a Redirect rule, so that it cuts the query string and redirects to the URL before the query string. For example, both of those URLs have to redirect to example.com/blog/ so that URL does not contain any query string.
I was trying
RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
Also tried
RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got the message that page is not working, 'URL' redirected you too many times.
BTW, technology I am using is Gatsby with htaccess plugin.
To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.
For example, to remove the query string from /blog/?<query-string> you would do something like this:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(blog)/?$ /$1/ [QSD,R=302,L]
This matches the URL-path blog/ (trailing slash optional) and redirects to /blog/ (with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?
The preceding condition (RewriteCond directive) checks the QUERY_STRING server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).
The $1 backreference in the substitution string contains the value from the captured group in the preceding RewriteRule pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily write RewriteRule ^blog/?$ /blog/ [QSD,R,L] instead.
The QSD (Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).
If the request does not contain a query string then this rule does nothing (since the condition will fail).
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.
A look at your existing rules:
was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
By default, the relative substitution string (ie. blog/) is seen as relative to the directory that contains the .htaccess file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the form https://example.com/path/to/public_html/blog/.
Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got message that page is not working, 'url' redirected you too many times.
This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.
Remove any query string from any URL
What rule do i write, to remove query string from any URL.
Modify the RewriteRule pattern to match any URL and redirect to the same. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1 [QSD,R=302,L]
This needs to go at the top of the root .htaccess file before any existing rewrites.
If the .htaccess file is in a subdirectory (not the root) then you will need to do something like the following instead, since the $1 backreference (as used above) won't contain the complete root-relative URL-path.
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

Redirect dynamic URL of travel website using .htaccess?

How to redirect
tour.php?translit=golden-triangle-of-india-with-ranthambore
to
tour/golden-triangle-of-india-with-ranthambore.html
using .htaccess?
Try the following near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^translit=(golden-triangle-of-india-with-ranthambore)$
RewriteRule ^(tour)\.php$ /$1/%1.html? [R,L]
$1 is a backreference to the parenthesised subpattern in the RewriteRule pattern, and %1 is a backreference to the parenthesised subpattern in the last matched CondPattern. This is simply to avoid repetition in the RewriteRule substitution.
The trailing ? on the RewriteRule substitution removes the query string from the request that would otherwise be passed through to the substitution (target URL).
This is a temporary (302) redirect. Change R to R=301 if this is intended to be permanent, but only after you have confirmed it is working OK (since 301s are cached hard by the browser).

how to remove parts of a url with htaccess

My url contains a broken link due to the http:// prefix not being added in places. How would I replace this using mod_rewrite:
http://website.com/www.websitelink.com
should go here:
http://www.websitelink.com
RewriteRule ^www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
In other words, if your path is /www.websitelink.com (^ is start of string,$ is end of string; in regular expressions, dots are one-character wildcards and have to be escaped)
(and [NC] matching is not case sensitive - /WwW.webSiteLink.COM would match, too),
[R=301] redirect with status "301 (Moved Permanently)"
to http://www.websitelink.com/
and [L] leave processing (no more rewrite rules are processed).
Note that this will work regardless of the site's domain (would work e.g. for http://website.com/www.websitelink.com and http://www.website.com/www.websitelink.com )
If you want to match all the paths that end with your domain, drop the starting ^:
RewriteRule www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
and if you want to match even paths without www., make it optional:
RewriteRule (www\.)?websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
As #Litso noted, this won't match the path after the "domain-in-path"; this should match the trailing path:
RewriteRule (www\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
To match any subdomain:
RewriteRule ([a-z0-9.-]+\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
And to match any domain:
RewriteRule ([a-z0-9.-]+\.)?([a-z0-9.-]+)\.com/(.*)$ http://www.$1.com/$2 [R=301,NC,L]

Resources