Need to redirect last part of url using htaccess with parameters - .htaccess

#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.

Related

How do I redirect url to specific url using 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").

Change URL ending with Query String

I've been using a set of redirect rules for a while that have been working perfectly.
I've recently expanded a part of my website and need to change the ending of a certain URL.
Old URL: /clan/{query-string}/tracking/war
New URL: /clan/{query-string}/tracking/warlog
I've changed my .htaccess file so the new URL works, but I need the old URL to redirect to the new one.
Currently, this is how I'm redirecting in .htaccess:
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
RewriteCond %{THE_REQUEST} /clanTracking_main.php\?name=([^\s]+) [NC]
RewriteRule ^.+$ /clan/%1/tracking/warlog [L,R]
RewriteRule ^clan/([^/]+)/tracking/warlog clanTracking_main.php?name=$1 [L]
It works perfectly but I just need help with the redirection.
Thanks for your help in advance!
I'd say the first rule below is what you ask...
I also made some other modifications which appeared to make sense to me...
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
# redirect old to new
RewriteRule ^/?clan/([^/]+)/tracking/war$ /clan/$1/tracking/warlog [R=301]
# pick name from get argument and redirect
RewriteCond %{QUERY_STRING} (?:^|&)name=([^\s]+)(?:&) [NC]
RewriteRule ^/?clanTracking_main\.php$ /clan/%1/tracking/warlog [R=301]
# rewrite to php
RewriteRule ^/?clan/([^/]+)/tracking/warlog$ clanTracking_main.php?name=$1 [END]
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.
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]

Clean URL rewriting rule

right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"

Drupal .htaccess rewrite/substitution without redirect

I have a URL as follows: www.site.com/catalog/?type=6 which I need to rewrite to www.site.com/white-wines/white-cases.
What is important is that it is not a redirect, for example the URL should remain as www.site.com/white-wines/white-cases when visited, but show content from www.site.com/catalog/?type=6.
The current solution that I have performs a redirect and I'm struggling to work this out.
RewriteRule ^white-wines/white-cases$ http://www.site.co.uk/catalog/?type=6 [L]
Setting full address in the target implies a redirect if the target is different then the current hostname, not rewrite. Try root-relative path.
RewriteRule ^white-wines/white-cases$ /catalog/?type=6 [L]
You can't do a re-write to a different hostname.
Absolute URL
If an absolute URL is specified, mod_rewrite checks to
see whether the hostname matches the current host. If it does, the
scheme and hostname are stripped out and the resulting path is treated
as a URL-path. Otherwise, an external redirect is performed for the
given URL. To force an external redirect back to the current host, see
the [R] flag below.
Source: Apache Module mod_rewrite

Resources