Rewrite URL with query parameters in .htaccess - .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...

Related

HTACCESS How to "cut" URL at one point

I am new to .htaccess and I don't understand it well. Recently I have built the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
This was in the root public folder (example.com/.htaccess). But now I have to create second Rewrite and I want to make .htaccess file in example.com/api/v2/ folder. I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
What I want to achieve:
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
Addressing your existing rule first:
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
The first RewriteCond (condition) is entirely superfluous and can simply be removed. The second condition simply asserts that there is a slash after the v2 and this can be merged with the RewritRule pattern. So, the above is equivalent to a single RewriteRule directive as follows:
RewriteRule ^api/v2(/.*) /api/v2/api.php?input=$1 [L]
This would internally rewrite the request from /api/v2/test/test/123 to /api/v2/api.php?input=/test/test/123 - note the slash prefix on the input URL parameter value.
However, unless you have another .htaccess file in a subdirectory that also contains mod_rewrite directives then this will create a rewrite loop (500 error).
Also note that you should probably include the L flag here to prevent the request being further rewritten (if you have other directives).
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
I assume /api? is a typo and this should be /api.php?. Note also that the slash is omitted from the start of the URL parameter value (different to the rule above).
I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
This is the right idea, however, you need to be careful of rewrite loops (ie. 500 error response) since the rewritten URL is likely matching the regex you are trying to rewrite.
Try the following instead in the /api/v2/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !api\.php$
RewriteRule (.*) api.php?input=$1 [L]
The preceding RewriteCond directive checks that the request is not already for api.php, thus avoiding a rewrite loop, since the pattern .* will naturally match anything, including api.php itself.
You could avoid the additional condition by making the regex more specific. For example, if the requested URL-path cannot contain a dot then the above RewriteCond and RewriteRule directives can be written as a single directive:
RewriteRule ^([^.]*)$ api.php?input=$1 [L]
The regex [^.]* matches anything except a dot, so avoids matching api.php.
Alternatively, only match the characters that are permitted. For example, lowercase a-z, digits and slashes (which naturally excludes the dot), which covers your test string test/test/123:
RewriteRule ^([a-z0-9/]*)$ api.php?input=$1 [L]
Or, if there should always be 3 path segments, /<letters>/<letters>/<digits>, then be specific:
RewriteRule ^([a-z]+/[a-z]+/\d+)$ api.php?input=$1 [L]

Htaccess rewrite with question mark in condition

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

Remove parentheses from the URLs query string in rewrite rule

I would like to clean up the URL's by removing parentheses from all query strings.
I tried the following code, but couldn't get it to work.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ /$1$2$3 [R=301,L]
Here's an example of a URL:
http://www.example.com/blog/abc-post/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogname+(Blog+Name+New+York)
In order to match the query string you need to check the QUERY_STRING server variable in a RewriteCond directive.
Here are some ways of doing this:
1. Any number of parentheses - multiple redirects
For example, to remove any number of opening/closing parentheses in the query string part of the URL:
RewriteCond %{QUERY_STRING} (.*)[()]+(.*)
RewriteRule (.*) /$1?%1%2 [R,NE,L]
The NE flag is required in your example to avoid the %-encoded character (ie. %3A) being doubly encoded.
This will, however, result in multiple redirects, depending on the number of "groups" of parentheses. In your example, this will result in two redirects, because there a two "groups" of parentheses (a single parenthesis in each "group").
2. Any number of parentheses pairs - multiple (but fewer) redirects
If the parenthesis are always in matching pairs, then you can specifically check for the opening/closing parenthesis and potentially reduce the number of redirects.
RewriteCond %{QUERY_STRING} (.*)\((.*)\)(.*)
RewriteRule (.*) /$1?%1%2%3 [R,NE,L]
In your example, this results in a single redirect because there is just a single pair of parentheses. But /abc?foo=(bar)&one=(two) would result in two redirects.
3. Any number of parentheses - single redirect
This method performs multiple internal rewrites to remove the parentheses, followed by a single redirect once all the parentheses have been replaced:
# Remove parentheses from query string
RewriteCond %{QUERY_STRING} (.*)[()]+(.*)
RewriteRule (.*) /$1?%1%2 [E=REPLACED_PARENS:1,NE,L]
# Redirect to "clean" URL
RewriteCond %{ENV:REDIRECT_REPLACED_PARENS} 1
RewriteCond %{THE_REQUEST} ^GET\ /(.*)\?
RewriteRule ^ /%1 [R,NE,L]
The first rule internally rewrites the request and sets an environment variable if a replacement is required.
The second rule checks for this environment variable (note that REPLACED_PARENS becomes REDIRECT_REPLACED_PARENS after the first rewrite) and ultimately redirects to the cleaned URL. The URL-path is grabbed from the initial request (contained in the THE_REQUEST server variable) to avoid inadvertantly redirecting to the directory index (eg. index.php) when a bare directory is requested (or front-controller is used).

URL mod-rewriting

I want to mod_rewrite this Url:
Before:
website.altervista.org/page.php?name=value
After:
website.altervista.org/value
Solution:
RewriteCond %{REQUEST_URI} !page.php$
RewriteRule ^(.+)$ /page.php?name=$1 [L]
Explanation:
The mod_rewrite RewriteRule has 3 parameters:
Pattern
Substitution
Flags
Implemented as such:
RewriteRule pattern substitution [flags]
Starting at server root, enter the requested URL path in the RewriteRule "pattern" parameter, and the desired path in the "substitution" parameter. In this case:
RewriteRule ^(.+)$ /page.php?name=$1 [L]
If the URL varies and you don't want to (or can't) write a rule for every situation then use the regular expression ^(.+)$ to capture the dynamic value and inject it into your substituted path using the RE capture variable $1. The first set of parenthesis is $1, the second set is $2, etc. And capturing parenthesis can be nested.
^(.+)$ This regular expression can be read as: ^ at the start of the string, $ all the way to the end of the string, look for . any character + one or times and () capture that value into a variable.
Problem:
Even though we have the flag [L] (last rule evaluated), the mod_rewrite engine (behind the scenes) sends the newly constructed request /page.php?name=somevalue back through the mod_rewrite engine until no rules are met or, apparently, there are no changes to the request. Fortunately there is a supplimentary directive to expand on the conditional power provided by the RewriteRule called RewriteCond.
The mod_rewrite RewriteCond applies to the next occurring RewriteRule and also has 3 parameters:
Test String
Conditional Pattern
Flags (optional)
The Test String can be derived from a few sources. Often a Server Variable, relating to the current request, is used here as the subject of this condition.
The Conditional Pattern is, again, text or a regular expression, but has some additional special conditions that may be evaluated. Read the Apache online mod_rewrite documentation for a detailed explanation.
In this case: RewriteRule ^(.+)$ /page.php?name=$1 [L], our newly substituted request is sent back through mod_rewrite as /page.php?name=somevalue and matches our "catch-all" rule, therefore our original "somevalue" is lost and replaced with our newly requested resource page.php. To prevent our "catch all" from catching our "page.php" requests let's exclude it from the rule using RewriteCond.
RewriteCond %{REQUEST_URI} !page.php$
RewriteRule ^(.+)$ /page.php?name=$1 [L]
This RewriteCond can be read as: %{REQUEST_URI} get the requested resource and does it ! NOT $ end with page.php. If this condition is true, continue to the next condition or rule. If this condition is not true, skip this rule set and continue to the next rule set.

is this the right way to apply a single rewrite rule to multiple pages?

I'm trying to use .htaccess but i'm a bit lost at this point. I was wondering how would you do
a rewriting for multiple pages.
RewriteRule ^your-order/$ /page1.php,page2.php,page3.php [L]
or should i just do this:
RewriteRule ^your-order/$ /page1.php [L]
RewriteRule ^your-order/$ /page2.php [L]
RewriteRule ^your-order/$ /page3.php [L]
also i was wondering if rewriterule would still execute if the page has a parameter:
URL: page1.php?test=hello
RewriteRule ^your-order/$ /page1.php [L]
I am assuming from your question about the query string params that you actually have the concept of the rewrites backward. The first expression is the submitted URL (pageN.php) and the second one is where it should be redirected or rewritten (your-order). In that case, you need only one rule.
Unless you need to take special action if a query string parameter is present (like go to a different page entirely), you don't need to match the query string. Query strings are matched in separate RewriteCond conditions rather than in the RewriteRule.
RewriteEngine On
# Rewrite page1, page2, page3 to your-order
# Add as many other pages as necessary separated by |
# The QSA appends any additional querystring to your-order
RewriteRule ^(page1|page2|page3)\.php your-order [L,QSA]
If your pages are actually named with the number at the end (which I doubt), you could use this expression instead:
RewriteRule ^page[0-9]+\.php your-order [L,QSA]
In either case, if you want the end user's browser to be redirected to the your-order URL, rather than an internal and invisible rewrite, change [L,QSA] to [L,R,QSA]

Resources