How do I redirect url to specific url using htaccess? - .htaccess

I have urls like as below
1.https://www.examples.com/demo-url/first-demo-url
2.https://www.examples.com/demo-url/second-demo-url
There are multiple urls like as above. Now I want to redirect these URLs to
https://www.examples.com/demo-another-url
I can do these redirection by writing multiple 301 redirection in .htaccess file. But I want it should redirect in single 301 redirection.
like as
Redirect 301 https://www.examples.com/demo-url/* https://www.examples.com/demo-another-url
Is this possible?

We can only assume that "-demo-url" is a fixed string which can be used to recognize such requests? If so that probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?demo-url/.+-demo-url /demo-url [END]
Or, if it is the /demo-url folder which may act as recognition anchor that would be an approach:
RewriteEngine on
RewriteRule ^/?demo-url/.+ /demo-url [END]
If you can not name such a string pattern that can be used to recognize such requests then there obviously is no way you can implement a general rule.
This uses the rewriting module provided by most installations of the apache http server. It allows for much more flexibility than the alias module which provides the Redirect directive.
As said the rewrite module needs to be loaded and rewriting has to be enabled for that location (http host). You should prefer to place such rules in the actual http server's host configuration. Only if you have no access to that you should consider using a distributed configuration file (often named ".htaccess").

Related

Need to redirect last part of url using htaccess with parameters

#RewriteRule #htaccess
I have a url for eg: www.example.com/path1/path2/pdfname.pdf. i need to redirect this url to another with the pdf name without pdf extension like(pdfname). Redirect Url should be www.example.com/path3/viewpdf.php?param=pdfname.
Would appreciate your help, Thanks.
Assuming that "path1", "path2" and "path3" are all fixed, literal strings that probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?path1/path2/(.+)\.pdf$ /path3/viewpdf.php?param=$1 [L]
This keeps the URL visible in the browser unchanged, which usually is what is desired.
That rule will work likewise in the central http server's host configuration (which usually is preferred) or, if you do not have access to that, in a distributed configuration file (often called ".htaccess"). In the later case that file needs to be readable by the http server process and it has to be located in the DOCUMENT_ROOT folder of the processing http server's host.
If instead of an internal rewrite you really want to redirect the request (so change the URL actually visible in the browser), then that variant should do:
RewriteEngine on
RewriteRule ^/?path1/path2/(.+)\.pdf$ /path3/viewpdf.php?param=$1 [R=301,L]
Again no domain name (http host name) or protocol scheme has to be specified if they stay the same. The same hints as above apply.

Redirect too many in htaccess

Currently I'm trying to work on my .htaccess file
What I want to do is to redirect all request to https://example.com/
Means all request for different pages of the domain should be redirect to the homepage/landing page for now.
I tried to do this in .htaccess
Redirect 301 / https://example.com/
But this gives me an output like this
redirect too many times
Well, looks like your are also redirecting requests to https://example.com/ to https://example.com/, right? ;-)
You need to take care that you only apply your redirection rule if the incoming request does not already request exactly that URL you are trying to redirect to. Two approaches here:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ https://example.com/ [R=301,L]
Or just:
RewriteEngine on
RewriteRule ^/?.+ https://example.com/ [R=301,L]
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).

How can I mask a domain using .htaccess?

We have the following situation:
We would like to setup a domain masking to provide content from a project platform to an end user. The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com
Now when the end user enters https://player.domain-client.com/5432 he should get the contents of https://app.domainA.com/player/?=5432.
But the URL should remain https://player.domain-client.com/5432.
This masking should only by applied if the client subdomain contains player.
Could anybody point me to the right direction on how to setup the .htaccess so it does the correct masking?
The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com
Presumably the "project platform" has also been configured to accept requests to player.domain-client.com?
In which case, it should just be a matter of a simple internal rewrite (on the same host). Although, if you would ordinarily request the same URL-path at app.domainA.com , ie. app.domainA.com/5432, then there is nothing you need to do as the rewrite is already in place? Otherwise, try the following:
RewriteEngine On
# Rewrite any request for /<number> to player/?=<number>
RewriteCond %{HTTP_HOST} ^player\. [NC]
RewriteRule ^(\d+)$ player/?=$1 [L]
However, /player/?=5432 isn't the actual endpoint as this requires further rewriting by the system for it to "work". Perhaps you mean something like /player/index.php?=5432? (The query string is also a little weird as you are missing a parameter name? As written, this would possibly require manual parsing of the query string to extract the value?)
The condition (RewriteCond directive) ensures that only requests to the player subdomain are rewritten.
On WordPress you need to make sure these directives go before the WP front-controller. ie. Before the # BEGIN WordPress section. The order of directives in .htaccess is important.
However, if this is all being managed by WordPress then you can't simply create a rewrite in .htaccess since WordPress still sees the original URL that was requested, not the rewritten URL. So, unless the requested URL exists as a valid route in WordPress itself then you'll likely get a 404. This sort of rewrite needs to be managed inside WordPress itself.
Alternative solution using a reverse proxy
An alternative is to configure your server as a reverse proxy and proxy the request from https://player.domain-client.com/1234 to https://app.domainA.com/player/?vid=1234 (mentioned in comments). Ideally this requires access to the main server config to config properly (requires mod_proxy and ProxyPass, ProxyPassReverse directives set appropriate in the virtual host).
Then, in .htaccess you would do something like the following instead, making use of the P flag on the RewriteRule:
# Proxy any request for /<number> to player/?=<number>
# for the "player" subdomain only.
RewriteCond %{HTTP_HOST} ^player\. [NC]
RewriteRule ^(\d+)$ https://app.domainA.com/player/?vid=$1 [P]

.htaccess permanent redirect - browser strangely adds GET variable to URL

I am trying to implement a very basic redirect for specific pages with htaccess, however the browser adds a GET variable to the new URL after the redirect:
Redirect 301 /branding/ABCDE http://example.com/branding/NEW
New URL in browser:
http://example.com/branding/NEW?slug=ABCDE
How can I correct this so that the ?slug=ABCDE does not show up?
Without seeing your .htaccess file, the best guess is that you have a conflict with other (specifically mod_rewrite) directives in your .htaccess file. There is nothing actually wrong with the Redirect directive itself that you posted.
The Redirect directive is a mod_alias directive and executes after mod_rewrite, despite the apparent order of these directives in your .htaccess file. A URL of the form /branding/ABCDE most probably requires rewriting (using mod_rewrite) for it to be useful by your application. This rewrite occurs before the Redirect, but any query string will be maintained.
It is not recommended to mix both Redirect and RewriteRule directives in the same config file, because of these unexpected conflicts.
Change your redirect to use mod_rewrite instead and make sure this is near the top of your config file. For example:
RewriteEngine On
RewriteRule ^branding/ABCDE$ /branding/NEW [QSD,R=301,L]
You will need to ensure your browser cache is cleared before testing, since the erroneous 301 (permanent) redirect will likely have been cached by your browser. To avoid caching issues, it is advisable to first test with 302 (temporary) redirects.
The QSD flag (Apache 2.4+) will discard any query string that might be present on the initial request.

301 Redirect Rules for a Blog Migration

I want to implement 3 redirect rules for a blog migration where each page will be shifted to a sub-folder structure. It currently sits at a sub-domain.
I can't really screw this one up and want to make sure I nail the correct generic rules for the 3 type of existing URLs:
Homepage
Current:
https://blog.lang.example.com
Goal:
https://www.example.com/lang-country/news/
Category
Current:
https://blog.lang.example.com/category/category-name
Goal:
https://www.example.com/lang-country/news/category/category-name
Post
Current:
https://blog.lang.example.com/yyy/mm/dd/article-name
Goal:
https://www.example.com/lang-country/news/yyy/mm/dd/article-name
Is this something you can help?
Unless you have other URLs that you don't want to be redirected then you can do something like what you require with a single redirect near the top of the .htaccess file in the subdomain's document root.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.([^.]+)\.example\.com [NC]
RewriteRule (.*) https://www.example.com/%1-country/news/$1 [R,L]
Where %1 is a backreference to the lang subdomain in the requested host.
However, you still have the problem of where country should come from in the target URL, since this is not present in the source URL. You will either need to default this to something or implement some kind of lookup based on the language. This would need server config access (to configure a RewriteMap) if you wanted to do this in .htaccess. Or, you implement this redirect entirely in your server-side script (eg. PHP).
Note that this is currently a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect once you have tested that everything is working OK (if a 301 is required). 301 redirects are cached hard by the browser so can make testing problematic.

Resources