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

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]

Related

Apache .htaccess: Redirect to an URL containing a hashtag

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.

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

Trying to exclude a URL from htaccess rule

I'm trying to setup a global mobile redirect in my httpd.conf for all of my sites on the server and only have to edit it once, vs. 92 times. All sites mobile will redirect to one locate so that's fine.
However I want to exclude one particular URL from redirecting at all. Here's what I have so far that isn't working and causing a redirect loop:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*iphone|ipod|blackberry|android|sgh|sonyericsson|psp|mot|htc|lg|nokia|palm|treo|j2me|webos|smartphone|symbian.*$ [NC]
RewriteCond %{REQUEST_URI} !^http://www.example.com/mobile [NC]
RewriteRule ^(.*) http://www.example.com/mobile [R=302,L]
Can anyone figure out why it's causing a redirect loop on that particular url? It's just a folder on that domain with a .htaccess in it only containing RewriteEngine off and an index.php file with a header() redirect call.
The %{REQUEST_URI} variable will never look like http://www.example.com/mobile, because it will never contain protocol (the "http://" bit) or host information (the "www.example.com"` part). You only want the URI part:
RewriteCond %{REQUEST_URI} !^/mobile
If you need to check the hostname as well, you need an additional condition:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]

Apache redirect question to change domain name

I am attempting to add a rewrite rule to my apache configuration file to redirect users to a new url.
The url of my site is https://openmind.scribesoftware.com. If a user enters a URL with https://openmind.scribesoft.com (note the lack of "ware") I'd like to redirect them as though they had typed the proper url.
I have tried a couple variations such as:
RewriteEngine on
RewriteCond %{HTTPS_HOST} !^openmind\.scribesoft\.com$ [NC]
RewriteRule ^(.*)$ https://openmind.scribesoftware.com/$1 [R=301,L]
However that results in the following error:
This webpage has a redirect loop.
The webpage at https://openmind.scribesoftware.com//enterprises/571 has resulted in too many redirects.
I already have a rewrite rule to redirect non-http requests to https requests and that is working fine.
Thanks.
HTTPS_HOST is not a real variable (see the RewriteCond docs). Use HTTP_HOST and HTTPS:
...
RewriteCond %{HTTP_HOST} !^openmind\.scribesoftware\.com$ [NC]
RewriteCond %{HTTPS} =on
...