.htaccess permanent redirect with query string - .htaccess

I want to have a permanent redirect where whoever visits the old url is automatically sent to the new one:
From: https://example.io/users?username=value
To: https://example.io/value
The new URL already works, but I just need the redirect. I already have this code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ users.php?username=$1 [QSA,L]

I'd say this should roughly be what your question asks for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^username=(.+)$
RewriteRule ^/?users\.php/?$ /%1 [R=301]
Those lines should work likewise in the http servers host configuration or in dynamic configuration files (".htaccess").
It redirects all incoming requests to the old URL "/users.php?username=value. I fail to see how the code you posted should help here, which is why I wrote above rules from scratch instead of modifying yours.
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).

Related

Mod-rewrite rule which just gets the part of the URL after the =

I'm currently trying to redirect this URL
http://dev.example.org/active/researchers/contact.php?IDENT=12345
to
http://portaldev.example.org/users/ident/12345
in htaccess.
However, I can only get a redirect to
http://portaldev.example.org/users/ident/IDENT=12345
because I can't find a way to get rid of the IDENT=. How can I do that?
The rewrite conditions in my htaccess are:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=([0-9]*)$
RewriteRule ^(.*)$ http://portaldev.example.org/users/ident/$2 [R=302,NC,L]
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)IDENT=(\d+)(?:&|$)
RewriteRule ^/?active/researchers/contact\.php$ http://portaldev.example.org/users/ident/%1 [R=302,QSD,NC]
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...
This implementation will work likewise in the http servers host configuration or inside a distributed 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 distributed 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 distributed configuration files (".htaccess"). Those distributed 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).
Got the answer to my question for anyone else who needs help:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=(.*)
RewriteRule (.*) http://portaldev.cepr.org/users/ident/%1? [R=301,L]
I think the main thing was the ? at the end of the rewrite rule but it also wouldn't work unless I put in the first RewriteCond to request uri and used (.*) in the rewrite rule

How can I Redirect subdomain with path to root domain without this path

I need help with this case
I need to redirect (301) a subdomain test.example.com/?preview_theme=niss-promag
to https://example.com without /?preview_theme=niss-promag
I already used code but it's redirecting to root domain with the same path https://example.com/?preview_theme=niss-promag
This is the code currently used:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ https://example.com [L,R=301]
Note: I need to redirect whatever path of the subdomain to root domain I don't specify the one above!
This should be working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com [QSD,R=301]
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...
If that does not work then most likely your http server is outdated, try something like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com/? [R=301]
This implementation 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).

How to redirect if URL contains certain word?

Project has folder API where two files named index.php and mobile.php.
index.php is contains APIs for web platform and mobile.php for mobile devices.
I have this problem. If code will do response to /api/get-courses, htaccess should redirect to index.php file. If response url has /api/mobile.php/get-courses mobile.php word, it shoul be redirected to mobile.php
RewriteEngine On
RewriteCond %{REQUEST_URI} mobile\.php
RewriteRule ^ mobile.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
There i have tried to do implementation.
It is a bit unclear what you actually mean by "If code will do response to ..." ... If that refers to a request to that URL which your want to internally rewrite, then this should do what you ask:
RewriteEngine on
RewriteRule ^/?api/get-courses$ /index.php [END,QSA]
RewriteRule ^/?api/mobile\.php/get-courses$ /mobile.php [END,QSA]
This implements an internal rewrite, not a redirection. I assume this is what you actually want, according to the wording in your question. If I miss understood what you actually ask then you need to take the time to revise your question to be more precise...
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.
This rule 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).

One website / three languages / three domains

I've got one website with three languages and three domains:
cheese.org, fromage.org and kaese.org
I want to create the following redirects (whith wildcards *)
cheese.org/fr/* redirects to fromage.org
cheese.org/de/* redirects to kaese.org
fromage.org/en/* redirects to cheese.org
fromage.org/de/* redirects to kaese.org
kaese.org/en/* redirects to cheese.org
kaese.org/fr/* redirects to fromage.org
I fiddled around with htaccess redirect (multidomain multilanguage) subfolder wildcard but didn't get it right.
Any suggestions?
Seems pretty straight forward... Depending on the http host you need to redirect incoming requests:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cheese\.org$ [OR]
RewriteCond %{HTTP_HOST} ^kaese\.org$
RewriteCond ^/?fr/(.*) https://fromage.org/$1 [R=301]
RewriteCond %{HTTP_HOST} ^fromage\.org$ [OR]
RewriteCond %{HTTP_HOST} ^kaese\.org$
RewriteCond ^/?en/(.*) https://cheese.org/$1 [R=301]
RewriteCond %{HTTP_HOST} ^cheese\.org$ [OR]
RewriteCond %{HTTP_HOST} ^fromage\.org$
RewriteCond ^/?de/(.*) https://kaese.org/$1 [R=301]
This rule 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 a certain page on domain A to a certain page on domain B

How would one redirect a certain page on domain A to a certain page on domain B in an htaccess file? We own both domains and both have nameservers pointing to a single server.
For example:
domainA.com/testpage
should go to
domainB.com/bestpageever
The following is as far as I have gotten. (I'm also forcing the www in front of the domain)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domainA.com$
RewriteRule ^(.*)$ http://www.domainB.com/bestpageever [R=301,L]
This happens to redirect all pages on domainA to the specified page on domainB, which is not what I want. No matter what kind of variation I try, I can't seem to get it to redirect just "testpage" on domainA to "bestpageever" on domainB.
This probably is close to what you are looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainA\.com$
RewriteRule ^/?testpage$ http://www.domainB.com/bestpageever [R=301]
The above rule will work in the http servers host configuration or in dynamic configuration files (.htaccess style files).
If you place such rules in the http servers host configuration you can simplify those rules to this, since then the rules are only applied inside the specific http host they are configured in:
RewriteEngine On
RewriteRule ^/testpage$ http://www.domainB.com/bestpageever [R=301]
A general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Resources