htaccess url parameter rewrite for specific page as subdomain - apache

I want to change my specific page URL as subdomain by .htaccess from
example.com/page.php to page.example.com.

We need to make some assumptions, since you've not included this information in the question:
You are requesting (ie. linking to) page.example.com and you wish to serve /page.php.
Both the subdomain page.example.com and domain apex example.com point to the same area of the filesystem, so both hostnames serve the same content.
In which case it is a relatively straight forward internal rewrite from page.example.com to page.example.com/page.php (based on assumption #2 above).
In your .htaccess file, you can achieve this using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(page)\.example\.com
RewriteRule ^$ %1.php [L]
The %1 backreference simply holds the value page from the preceding CondPattern.
This only matches requests for https://page.example.com/ and not https://page.example.com/<something>

Related

Redirect >> domain.com/test to another url (domain.com/test3 using .htaccess (wp)

I am trying to redirect one complete URL
But this is not working ...
RewriteCond %{HTTP_HOST} ^https://www.moccioso.com/d.php?
id=ultimateselection.zip$
RewriteRule ^$ http://moccioso.com/sd_product/ultimateselection-zip
[L,R=301]
(I'm assuming you've not wrapped the code onto multiple lines in your actual code?)
The HTTP_HOST variable contains the Host HTTP request header. ie. the hostname specified on the request eg. www.example.com only - there is no scheme, URL-path or query string here. So, this RewriteCond directive will never match.
But you are also trying to match an empty URL-path with the RewriteRule pattern ^$ - so again, this will never match. In your example the URL-path is /d.php. The RewriteRule pattern matches against the URL-path only.
You are also redirecting to http (not https) in this directive? Your example uses https. You are also removing the www subdomain, but again, your example includes it?
If you are redirecting from/to the same hostname and only have one domain then you don't need to explicitly include the domain name (ie. the requested hostname) in the rule.
To match against the query string you need to match against the QUERY_STRING server variable.
Try the following instead, near the top of your .htaccess file.
RewriteCond %{QUERY_STRING} ^id=ultimateselection\.zip$
RewriteRule ^d\.php$ /sd_product/ultimateselection-zip [R=301,L]
This will need to go near the top of your .htaccess file. Test first with a 302 (temporary) redirect to avoid potential caching issues.
Clear your browser cache before testing.

.htaccess redirect if contains string keeping some parameters

I'm trying to redirect a route to a subdomain if it contains a certain string:
For example, if domain contains /foo/bar/:
http://example.com/foo/bar/123/456.jpg should be redirected to the subdomain http://sub.example.com/bar/123/456.jpg.
I mean, I would need to remove only /foo/ from the route and redirect the result to a subdomain.
I don't know how to remove a certain string in the middle of the route keeping following strings.
Try something like the following near the top of the .htaccess file in the root of example.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^foo/(bar/\d+/\d+\.jpg)$ http://sub.example.com/$1 [R,L]
If the subdomain points to a completely different area of the filesystem then you can remove the RewriteCond directive that checks the hostname. Or if the /foo/ prefix should always be removed (regardless of which host is requested).
This matches requests of the form /foo/bar/<number>/<number>.jpg.
$1 is a backreference to the captured group in the RewriteRule pattern that specifically omits the foo/ prefix.
This is a temporary (302) redirect.
if domain contains /foo/bar/
That is part of the URL-path, not the domain.

.htaccess - Force rule from root .htaccess file despite each subdirectory having own file?

The Problem
I have a piece of code from another Q&A that is a generalized solution to forcing www in web addresses:
# Force www.
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
My understanding of the logic:
If the HTTP_HOST is defined solely as "(no period string).(no period string)", ie. example.com, then capture the remainder and rebuild the URL with www affixed to the head of the HTTP_POST and captured string.
I have placed this code in a .htaccess file in my web document root. Within that folder are a number of subdirectories which each contain their own .htaccess file to handle redirects and clean urls that are automatically managed by a CMS. All of the entries in these subdirectories tend to follow the following pattern:
RewriteRule ^$ /reason/displayers/generate_page.php?site_id=11111&page_id=222222
RewriteRule ^page$ /subdirectory/page/ [R=permanent]
RewriteRule ^page/$ /reason/displayers/generate_page.php?site_id=111111&page_id=333333
The first problem I have is that the RewriteRule in the solution only triggers for the root page of the website and not any subdirectory or pages:
http(s)://example.com ->http(s)://www.example.com (Good)
http(s)://example.com/subdirectory/ ->http(s)://example.com/subdirectory/ (Bad)
http(s)://example.com/subdirectory/page/ ->http(s)://example.com/subdirectory/page/ (Bad)
Looking at another Q&A, a user suggested adding RewriteOptions inherit to the subdirectory .htaccess. Doing that with my initial RewriteRule in a single test directory results in the following:
http(s)://example.com/subdirectory/page/ ->http(s)://example.com//reason/displayers/generate_page.php/events/?site_id=111111&page_id=333333
The page content does appear, but the URL is certainly less than desirable. The OP of that Q&A left a comment noting that they needed to use %{REQUEST_URI} rather than attempting to capture and reuse it. Using that for the RewriteRule, I get:
http(s)://example.com/subdirectory/page/ ->http(s)://www.example.com/subdirectory/page/?site_id=111111&page_id=333333
Close, but I certainly don't want the parameters to appear there for the sake of SEO.
Questions
Is there any way to apply this kind of rule to all subdirectories and pages without needing to go in and add the RewriteOptions inherit line to each subdirectory .htaccess? Given these are created dynamically by the CMS each time a site is created, manually managing the inclusion of that line isn't ideal.
Assuming that, is there any solution to resolve the "bad" URLs I receive from my attempt to utilize RewriteOptions inherit given what the CMS is placing into the subdirectories automatically?
Alternative
This last one is likely outside of this site's scope, but given it's related, I included it in case the preceding isn't doable or if the following is preferable:
Would it be possible to handle this kind of redirect using VirtualHosts entries? As far as I can tell in my case, both "example.com" and "www.example.com" resolve to the same IP and document root. If they could be separated, www.example.com could remain as is while example.com is set to go somewhere else, allowing for a clear redirect to occur, perhaps?
<VirtualHost 111.11.11.111:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/
...
</VirtualHost>
Yes indeed, to apply your rule to all the sub-directories insert this rule in <VirtualHost..> section:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

Apache Mod Rewrite - Replace www.example.com with www/example.com

First what I have that already works...
On my server I use parked domains where each domain points to the same directory and the software merely determines what the domain is before pulling from the similar named database. For files (images, scripts, etc) I currently have the following setup...
public_html/www.example1.com/
public_html/www.example2.com/
public_html/www.example2.com/
...using the following rewrite...
RewriteCond %{REQUEST_URI} !\.(gif|jpg|js|mp3|png)$
RewriteRule ^[^/]*/(images|search|scripts|sounds)(.+) $1$2 [QSA]
So in example if you request image77.png from example2.com Apache rewrites the (internal) request to...
public_html/www.example2.com/images/image77.png
The external path (in your browser) would simply appear as...
http://www.example2.com/images/image77.png
Secondly, the problem I'm trying to solve...
However as I add more domains this is starting to crowd my *public_html/* directory. What I'm trying to do is simply move all the domain asset directories in to a directory setup that looks like the following...
public_html/www/
...so the www directory would have the following listing...
public_html/www/example1.com/
public_html/www/example2.com/
public_html/www/example2.com/
Lastly, my thoughts on how to possibly approach the problem...
The first Apache variable $1 is generated from %{REQUEST_URI}. If we can simply replace the period . between www and the domain name this should update the rewrite to the newer format though I'm not sure how to do that and keep what works already in tact?
In other words can we can create a rewrite condition to replace just the first period with a slash?
You can capture the domain part after www. and use it later in the RewriteRule as %1
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^.*?/(.*)$ /public_html/www/%1/$1 [L]