Apache .htaccess: Redirect to an URL containing a hashtag - apache

The URLs
https://example.com/moved/
https://www.example.com/moved/
should be redirected to the URL
https://my.example.com/#views/settings.php?id=2
via .htaccess.
This is what I have tried:
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} moved$
RewriteRule (.*)$ https://my.example.com/#views/settings.php?id=2 [R=301,L]
However it seems not to work. I guess the reason is the #hashtag within the URL where I want to redirect to (it marks a comment within .htaccess).
How to redirect this correctly?

Change your rule to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^moved/?$ https://my.example.com/#views/settings.php?id=2 [R=301,L,NE,NC]
^(?:www\.)?example\.com$ will match www.example.com and example.com
Flag NE is used for no escaping of # in redirected URL
Pattern moved/?$ matches /moved/ and /moved, thus making trailing slash optional.
Make sure to clear your browser cache or use a new browser for testing.

Related

htaccess redirect from one port to another

I have want to redirect urls that contains ?_escaped_fragment_= to another port. For an example,
http://localhost:8080/web/?_escaped_fragment_=/privacy
to
http://localhost:8082/web/?_escaped_fragment_=/privacy
My current htaccess is as below.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ http://dev.dermveda.com:8082/$1 [r=301,nc]
The issue is, if the URL contains web/, it doesnt redirect to my new port. But if it doesnt have web/ it redirects fine.
e.g.
URLs like http://localhost:8080/web/?_escaped_fragment_=/privacyare not redirected. But URLs like http://localhost:8080/?_escaped_fragment_=/privacy are redirected. My question is, How should I implement htaccess to redirect URLs with web/ to another port.
Problem is presence of this condition:
RewriteCond %{REQUEST_URI} ^/$
This condition means redirect only if your URL is /, which means only landing match.
Just remove that condition and have your rule as:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_= [NC]
RewriteRule ^ http://dev.dermveda.com:8082%{REQUEST_URI} [R=301,L,NE]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

Redirecting a URL from one domain to another in Apache containing character #

I was trying to use Apache mod_rewrite for redirecting URLs from one domain (e.g. pesdevelopment.ktm.local) to another domain (pestest.ktm.local).
Therefore I have used the following condition and rule within Apache:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^pesdevelopment\.ktm\.local$ [NC]
RewriteRule ^(.*)$ http://pestest.ktm.local/$1 [R=302,L]
The problem is that URLs that are containing the character # are not redirected correctly. The character is stopping the rewrite process and it is redirected to the URL that was rewritten so far until the character has appeared in the URL.
Example:
The URL pesdevelopment.ktm.local/Windchill/app/#ptc1/ is redirected to pestest.ktm.local/Windchill/app/
Does anyone know how to handle this situation?
The following should work
RewriteEngine on
RewriteCond %{HTTP_HOST} ^pesdevelopment\.ktm\.local$ [NC]
RewriteRule ^(.*)$ http://pestest.ktm.local/$1%1 [R=302,L]

.htaccess RewriteRule makes the address bar url changes when there isn't an end slash

I have an strange situation with .htaccess configuration.
I have a multidomain hosting, with one directory per each domain.
For example, I have:
http://www.domain1.com
http://www.domain2.com
and the following directory structure:
/domain1/
/domain1/index.php
/domain2/
/domain2/index.php
/domain2/subdirectory/
/domain2/subdirectory/index.php
and a .htaccess file with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain1/.*$
RewriteRule ^(.*)$ domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain2/.*$
RewriteRule ^(.*)$ domain2/$1 [L]
all works as I like with one exception:
if I do:
http://www.domain2.com/subdirectory/
(with a slash at the end)
the content is showed propertly and the url on the address bar keep unchanged, but
if I do:
http://www.domain2.com/subdirectory
(without a slash at the end)
the content is showed properly but the url on the address bar is changed to:
http://www.domain2.com/domain2/subdirectory/
(notice the subdirectory name and a slash is appended at the end)
and I don't like the user see that change on the address bar, that's my problem
This is mod_dir and DirectorySlash interferring with the URI-file mapping pipeline. By default DirectorySlash is turned on, which makes it so when apache thinks a request is for a directory, and it's missing the trailing slash, then it 301 redirects to the URI with the trailing slash. There's a good reason for doing this because of a possible information disclosure security concern. If you don't allow for directory indexing, you could just turn it off:
DirectorySlash Off
And then Apache won't redirect to the trailing slash at all. Or you can do the redirecting yourself as part of the rewrite engine. The reason why your internally rewritten URL is exposed is because 2 modules are mangling the URI, mod_dir and mod_rewrite, and they're doing it without any awareness of each other. So the URI gets rewritten by mod_rewrite and then mod_dir comes along and says "this is a directory!", mangles the URI some more by adding a trailing slash and flags it for a redirect response. Eventually, what you get is a redirect with a mangled URI. By handling the redirect from within mod_rewrite, by the time it gets to mod_dir, everything's peachy.
These would need to be above your current rules
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain1/.*$
RewriteCond %{DOCUMENT_ROOT}/domain1%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain2/.*$
RewriteCond %{DOCUMENT_ROOT}/domain2%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

How to redirect request coming from a domain to a subdirectory invisibly?

I am trying to redirect all requests coming from example.com to a subdirectory. The code below accomplishes this but I cannot make the subdirectory invisible. (The subdirectory contains a Drupal instance with its own/htaccess file. Could that be the problem?)
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/drupal/d6/$1 [L]
This ends up looking like this in the browser:
http://example.com/drupal/d6/install.php?profile=default
EDIT: I tried removing http://example.com from the RewriteRule, as recommended, but the redirect is still not invisible. :-(
You need to rewrite to the location, not to a full http:// URL. If you do it to a URL then a redirect is issued even without the [R] flag on the RewriteRule. Go instead to /drupal/d6/$1 and it should stay a rewrite (rather than "upgrading" to a redirect).
From http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection.
An absolute URl will cause a redirect. Use just a path:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond $0 !^drupal/d6/
RewriteRule .* drupal/d6/$0 [L]