.htaccess RewriteRule based on anchors - apache

I am trying to update a RewriteRule. Previously, the redirect looked like this:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
I would like to use /docs/ for something else now but I would like to keep redirecting requests containing a forward slash after the # to the manual subdomain. This is what I would like to achieve:
The old redirects continue working as usual:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
This would not get redirected since there is no forward slash following the #:
https://mywebsite.com/docs/#overview
Here is what I have:
The .htaccess file containing the following existing rule which redirects everything:
RewriteRule ^docs/(.*) https://manual.mywebsite.com/$1 [L,R=301]
I tried this but it did not work (I tried with https://htaccess.madewithlove.com/ which says that my rule does not match the URL I entered):
RewriteRule ^docs/#/(.*) https://manual.mywebsite.com/#/$1 [L,R=301]
I also read about the NE (no escape) flag (https://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors) which did not help either.
I am also sure that the server is actually using the file.
To summarize, my problem is that I want to match a URL containing /docs/#/ and redirect it to a subdomain, keeping the /#/ and everything that follows it.

Anchors are not part of the URL that's transmitted to the server: They stay within the browser, thus you can't build a rewrite rule that take anchors into account.
Inspect your access-logs to see what you get

Related

How to permanently append X to complete a shorthand URL via htaccess rewrite for a bi lingual site?

Using htaccess how can one achieve the following on a bilingual (the only two languages being: en|nl)?
if typed website.org/en (without anything after /en) then permanently redirect to website.org/en/home
if typed website.org/nl (without anything after /nl) then permanently redirect to website.org/nl/home
However, I dont want to just randomly add /home after any /* So this is only to catch incomplete shorthand urls /en and /nl, since normally all other urls always have /en/some page name.
You can use this rule as your topmost rule:
RewriteEngine On
RewriteRule ^(en|nl)/?$ /$1/home [L,NC,R=301]

Apache RedirectMatch

I'm working on an apache server (2.2), and I'm trying to redirect a URL based off of a URL filter. For example,
https://mywebsite.com/path/to/page?folder=folderDirectory/folderName
will redirect to:
https://mywebsite.com/static/contentUnavailable.html
In my httpd.conf file I have the following code ..
RedirectMatch (.*)path/to/page?folder=folderDirectory/folderName /static/contentUnavailable.html
I restart apache everytime I make modifications to this file, however the page is not redirecting. What am I doing wrong in the RedirectMatch?
You can't match query string with a redirectmatch, sorry, you need mod_rewrite for this and using a RewriteCond. Rough example:
RewriteCond %{QUERY_STRING} ^folder
RewriteRule ^ /static/contentUnavailable.html [R,L,QSD]
This will match a query string that starts with folder (and continues with whatever else, no matter what it is). and redirect everything to the destination you want, discarding the query string in the process (QSD flag).
In any case let me commend you for trying to stick to redirect/redirectmatch first (while everyone else just goes blindly for mod_rewrite even for the simplest redirects). You are doing things right.

How to include a single $ (dollar-sign) within the rewrite rule

I'm trying to create a rewrite rule for my .htaccess file. I want to include a single dollar sign within it. It is supposed to be something like this:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^\$$ "http\:\/\/domain\.com\/something" [R=301,L]
The problem is that it doesn't work with a single dollar sign, so if I go to domain.com/$ I get a 404. It works only with another letter, for example ^\$a$ - in such case domain.com/$a would redirect to domain.com/something.
The workaround is to create a new folder, rename it to $ and put the .htaccess file there, but I would rather avoid creating multiple folders with no content for such purposes. I couldn't find any reference on the Internet (the official Apache documentation for mod_rewrite was not very helpful). I tried using multiple slashes in different combinations but everything failed. Am I missing something or is it just impossible to make it work this way?
For me it works exactly as expected in htaccess with RewriteRule ^\$$ and requesting "$". Have you looked at the rewritelog / loglevel rewrite:trace8 yet?

multiple folder redirect

I have been trying variations of the following without success:
Redirect permanent /([0-9]+)/([0-9]+)/(.?).html http://example.com/($3)
It seems to have no effect. I have also tried rewrite with similar lack of results.
I want all links similar to: http://example.com/2002/10/some-long-title.html
to redirect the browser and spiders to: http://example.com/some-long-title
When I save this to my server, and visit a link with the nested folders, it just returns a 404 with the original URL unchanged in the address bar. What I want is the new location in the address bar (and the content of course).
I guess this is more or less what you are looking for:
RewriteEngine On
ReriteRule ^/([0-9]+)/([0-9]+)/(.?)\.html$ http://example.com/$3 [L,R=301]
This can be used inside the central apache configuration. If you have to use .htaccess files because you don't have access to the apache configuration then the syntax is slightly different.
Using mod_alias, you want the RedirectMatch, not the regular Redirect directive:
RedirectMatch permanent ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3
Your last grouping needs to be (.+) which means anything that's 1 character or more, what you had before, (.?) matches anything that is either 0 or 1 character. Also, the last backreference doesn't need the parentheses.
Using mod_rewrite, it looks similar:
RewriteEngine On
RewriteRule ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3 [L,R=301]

How do I make the browser display the clean URL on a re-write?

I have a hyperlink that looks like this:
http://domain.com/sample/comments/65
And when I click on it, it goes to this:
http://domain.com/sample/comments/index.php?submissionid=65
I'm using a rewrite rule to make it do this. This is what I want, except I also want the URL displayed in the browser to still look like "http://domain.com/sample/comments/65."
How can I do this? The .htaccess file is displayed below.
RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ http://domain.com/sample/comments/index.php?submissionid=$1 [NC,L]
Thanks in advance,
John
You must remove the part http://domain.com/sample/, otherwise it will force a redirect:
RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ comments/index.php?submissionid=$1 [NC,L,B]
The B flag is also necessary because you're using the backreference inside a query string, which requires escaping.
The manual says (emphasis mine):
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting; without this, you would always have to match the parent directory, which is not always possible. There is one exception: If a substitution string starts with http://, then the directory prefix will not be added, and an external redirect (or proxy throughput, if using flag P) is forced. See the RewriteBase directive for more information.
This would not be case if you put the rewrite rule in the virtual host or main configuration as long the request host and the host in the rewrite rule matched.