I need to create a redirect rule to lowercase.
All URLs starting with /catalog/ must be lowercase. example:
/catalog/Foo => /catalog/foo
/catalog/Foo/fOO2 => /catalog/foo/foo2
Can you help me?
Assuming you are on Apache 2.4 then you can use the tolower() function in an Apache expression in a mod_rewrite RewriteCond directive to perform the uppercase to lowercase conversion on these URLs.
For example:
RewriteEngine On
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule ^catalog/.*[A-Z] %1 [R=302,L]
The RewriteRule pattern checks for a URL-path that starts catalog/ followed by an uppercase character. The RewriteCond directive then converts the URL-path to lowercase, which is captured in the regex and the corresponding backreference %1 is used in the RewriteRule substitution string as the target for the redirect. %1 contains the lowercase'd URL-path.
Related
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...
I need to redirect subdomains like
^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW).example.com/uk/(.*)$
for example:
de.example.com/uk/
ceb.example.com/uk/
redirect to http://$1.example.com/en/
for example:
de.example.com/en/
ceb.example.com/en/
how to change in .htaccess?
I try to use following but not works:
RewriteRule ^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW)/uk http://$1.ts2.space/en [R=301,L]
The RewriteRule pattern matches against the URL-path only. To match the hostname, you need an additional condition (RewriteCond directive) and match against the HTTP_HOST server variable (ie. the value of the Host HTTP request header).
In your example, you are redirecting to the same host, so there is no need to use a backreference in the substitution and you can omit the scheme+hostname entirely.
For example, the following redirects de.example.com/uk/ to de.example.com/en/ as per your first example.
RewriteCond %{HTTP_HOST} ^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW)\.example\.com
RewriteRule ^uk/$ /en/ [R=302,L]
NB: Don't forget to backslash-escape literal dots in the regex.
Reference:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
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
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).
I have the following RewriteCond in htaccess:
#RewriteCond %{HTTP_HOST} ^xyzdomain.com$
Now I would like to use a placeholder for the string "domain" in xyzdomain.com so that the condition matches with any domain like xyzhome.com, xyztravel.com, xyzshop.com, xyz*.com, etc.
Which placeholder can I use for the string?
It's just a regex. You can do something like:
RewriteCond %{HTTP_HOST} ^xyz(.+)\.com$
The (.+) will match "any character one or more times".
Also note the \ escaping the . in .com. . is a special wildcard character with regular expressions so you have to escape it when you want to explicitly match ..
regular-expressions.info is a great place to learn more about how they work.