Htaccess redirect URL to another one and delete characters - apache

I want an old website to be redirect to the new one, the URL looks like this : https://test.fr/directory/homepage.html
In my .htaccess file I have :
RedirectPermanent / https://google.com/`
But what it does : https://google.com/directory/homepage.html
I'd like to redirect to https://google.com/ and delete everything after the .com/ but I have no idea how.

Just use the rewriting module:
RewriteEngine on
Rewrite ^ https://google.com/ [R=301,END]
Obviously the rewriting module needs to be loaded into the http server for that. This usually is the case, certainly for all hosting providers.
If you only want to redirect that specific URL inside your http host, then that should do:
RewriteEngine on
Rewrite ^/?directory/homepage\.html$ https://google.com/ [R=301,END]
You can implement such directives in a distributed configuration file (".htaccess"). You should prefer the actual http server's host configuration though, if you have access to that.

Related

redirect any link on any subdomain-url to another domain

I registered a expired domain to forward all incoming links to another domain. The problem is: many inlinks are placed on subdomains, for example: axa-art.cdn.contento-v41.eu/axa-art/0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
I am looking for a 301 redirect rule in htaccess that forward any url (no matter on main domain or subdomain) to "new-url.tld"
axa-art.cdn.contento-v41.eu
axa-art.cdn.contento-v41.eu/slug
any-subdomain.contento-v41.eu
any-subdomain.contento-v41.eu/slug
all of this example above should
forward to this exact URL: new-domain.tld
Question 1:
Is it possible to create a "general" rule and place it into htaccess of the main directory?
Question 2:
Or do i have to write a specific rule for each subdomain?
Question 3:
Do I have to create a sub-directory and create a separate htaccess in every sub-directory for each subdomain I want to add redirection-rules?
Help or suggestions are highly appreciated. Thank you very much for your help in advance.
This isn't just a .htaccess question. In order for your server to receive requests to <any-subdomain>.example.com the necessary DNS and server config directives need to be in place. If the request doesn't reach your server then you can't implement a redirect in .htaccess.
So, I suspect that these subdomains are not even resolving?
You either need to create the necessary DNS A records and ServerAlias directives one by one for each hostname (ie. subdomain) or create a "wildcard" DNS A record (and ServerAlias *.example.com directive in the vHost). But then you still have an issue with these hostnames being covered by an SSL cert if you need to redirect from HTTPS.
You can then create the necessary redirect in .htaccess. Although, since you need access to the server config (or a using a control panel that does this for you) to implement the directives above, you should also implement this redirect in the server config also.
For example, at the top of your .htaccess file, before the existing directives (or in your vHost):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^new\.example$
RewriteRule ^ https://new.example%{REQUEST_URI} [R=301,L]
The above states... for any request that is not for new.example then 301 redirect to https://new.example/<same-url>.
However, if you have access to the server config and this other domain is configured in its own vHost container then the redirect can be simplified:
Redirect 301 / https://new.example/
UPDATE#1:
this rule does forward any URL form the main domain to the new domain.
# Permanent URL redirect- by netgrade
RewriteEngine on
RewriteCond %{REQUEST_URI} !https://www.marco-mahling.de/$
RewriteRule $ https://www.marco-mahling.de/ [R=302,L]
The rule I posted above should probably replace your existing rule entirely.
Yes, your rule does redirect every URL to the root of the new domain, but it is arguably incorrect. The RewriteCond directive is superflous and isn't actually doing anything. The REQUEST_URI server variable contains the URL-path, it never contains the scheme + hostname. So, the RewriteCond directive you've posted will always be successful.
If that is the rule you currently have then it would already redirect everything. In which case your problem would seem to the necessary DNS and server config directives as mentioned above.
From your directives, I assume that the other domain actually points to a different server (or different vHost on the same server). Otherwise, this would have resulted in a redirect-loop. In which case, you only need the much simpler Redirect directive that I posted above.
UPDATE#2: That works fine BUT the incoming links are still not forwarded cuz of a "%" in the url: https://axa-art.cdn.contento-v41.eu/axa-art%2F0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
It's actually because of the %2F - an encoded slash (/) in the URL-path. By default, Apache will reject such URLs with a 404 (for security reasons).
To allow encoded slashes in the URL you would need to set AllowEncodedSlashes On in the server config (or vHost container). You cannot set this in .htaccess. (The server generated 404 occurs before .htaccess is even processed.)
However, I would express caution about enabling this feature. (Is there a specific requirement here? Are you recreating these documents on the new server?)
If this request was intended to map directly to a PDF file on disk then this actually looks like an incorrectly URL encoded request, since a slash / is not a permitted filename character on either Windows or Linux.
If you enable AllowEncodedSlashes then the above RewriteRule will redirect the request to /axa-art/0eee9cec....pdf - note the %-decoded / in the resulting URL. You would need to take additional steps to maintain the URL-encoding (if that was required), but as I say, that looks like a mistake to begin with.

Htaccess rewrite subfolder to subdomain

i have a domain with many subdomain; what i want to do is to map specific subdomains do specific subfolder.
let's suppose the domain is example.com, i have these domains:
example.com
www.example.com
demo1.example.com
demo2.example.com
and 2 subfolders in my var/www/html folder:
/var/www/html (for example.com and www.example.com)
/var/www/html/demo1 (for demo1.example.com)
/var/www/html/demo2 (for demo2.example.com)
So what i want to is set up the correct mapping so that:
opening (www.)example.com/demo1 should redirect to demo1.example.com
opening (www.)example.com/demo2 should redirect to demo2.example.com
opening (www.)example.com/anything-else should do nothing (content in /var/www/html)
Obviusly demo1.example.com should use the content in /var/www/html/demo1 and demo2.example.com should use the content in /var/www/html/demo2
You probably want to implement rules handling both directions:
1. redireting requests to the "www" host to the "subdomain" (actually another host name)
2. rewriting requests to the non-www hosts to a folder, if such folder exists
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?demo1/(.*)$ https://demo1.example.com/$1 [R=301]
RewriteRule ^/?demo2/(.*)$ https://demo2.example.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^demo1\.example\.com$
RewriteRule ^ /demo1/%{REQUEST_URI} [END,QSA]
RewriteCond %{HTTP_HOST} ^demo2\.example\.com$
RewriteRule ^ /demo2/%{REQUEST_URI} [END,QSA]
You could certainly implement a more generalized rule for the second part (the internal rewriting), but above explicit approach is easier to reach, thus easier to maintain. I prefer it for that reason.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect Without changing URL Apache

I want to redirect one URL to another without changing the Browser URL
www.example.com/abc/(.*).xml should redirect to www.example.com/abc/xyz/index.htm?file=$1
But the Browser should display www.example.com/abc/(.*).xml
You can use a RewriteRule:
RewriteEngine On
RewriteRule /abc/(.*)\.xml$ /abc/xyz/index.htm?file=$1 [L]
Make sure you have mod_rewrite enabled and put this either in your VirtualHost config, or in a .htaccess file in your DocumentRoot
As Constantine posted on the accepted solution, the [P] flag is dangerous as it converts the server as a proxy.
See [this]: https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever?noredirect=1&lq=1
P = Proxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-server fetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad.

How to redirect a page and disguise its url?

I need to do the following on my apache webserver :
Redirect any URL starting with :
http://mydomain1.com/archive
to
http://mydomain2.com/archive
Is there a way using mod-rewrite or RewriteEngine to disguise that URL, so that the URL that appears in the browser is mydomain1.com ? I don't want to give away the fact that we are switching servers.
you could try a reverse proxy. This will allow you to take one url and forward the request to another server without the end user knowing.
Try adding this in the .htaccess from domain1.com/archive
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule (.*) http://mydomain2.com/archive [P]
</IfModule>
Edit: Tell me if you've tried this and if it worked or not.
That's not possible using mod_rewrite. If host changes then it has to be an external redirect using R flag.
A possible workaround is to make server to server call from inside your code on mydomain1 to mydomain2. If using php you can use file_get_contents function to make this happen.

How to configure apache (ubuntu) such that www.mysite.com will direct to www.mysite.com/drupal6/?

I am a newbie to ubuntu and apache. Can someone tell me how I could direct to
www.mysite.com/drupal6
when user address www.mysite.com?
Thanks a lot.
Cheers.
If you are running Apache and Ubuntu, there is actually a really easy way to force this redirect using a simple php script.
Create an index.php file in the root of your server and paste the following code into it
<?php header("location: drupal6/") ?>
This will cause the site to auto-redirect to the drupal6 folder whenever it is visited.
This should work. Create a file in the root folder of your server called .htaccess - the dot at the beginning is very important as this helps the server identify the file as a hidden / system config file.
Open the file and paste the following lines of code in :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mysite.com/drupal6/$1 [R,L]
This should force all traffic to the server to redirect to your custom folder.
A brief explanation of the .htaccess code
If you want rewrites to work, you have to enable the Rewrite Engine and tell the server to follow symlinks.
The second section establishes the rule - specifically applying it to all traffic on the standard web port of 80.
The final line tells the server to grab everything after the URL and append it to the new address (mysite.com/drupal6).
There's a lot more you can do with .htaccess files but you really need to Google for good examples to test out.
Look at Apache's mod_rewrite documentation. You will need a RewriteRule in your apache configuration at the minimum, you may also need RewriteCond's to define when the RewriteRule is used.
Your rewrite pattern will be rewriting the REQUEST_URI with something from: ^/$ to: /drupal6. The ^ and $ are essential to prevent Apache getting into an infinite loop while rewriting the base URI by only matching "/" and not "/anything-else".
I assume you're on a recent version of Ubuntu and Apache? If so, see the Apache 2.2 documentation on mod_rewrite.