Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Test the hostname in RewriteCond %{HTTP_HOST}, then match the requested path in RewriteRule. In a document-root .htaccess file, omit the leading slash from the rule pattern. Use a relative substitution for an internal subdomain-to-directory mapping, or an absolute URL with an intentional redirect status when the browser must move to another host.

The rule model: hostname in a condition, path in the rule

Apache evaluates a per-directory rewrite in two separate parts. RewriteCond decides whether the request has the characteristics you want, while RewriteRule matches the URL path and supplies the replacement. The hostname is not available to the rule pattern itself. Apache’s documented behavior is: “The Pattern is matched only against the URL-path — not the hostname, port, or query string.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a request such as https://app.example.com/docs/page.html, a condition can test app.example.com, and the rule pattern sees a path such as docs/page.html. The query string is handled separately; it is not part of the RewriteRule pattern.

In .htaccess, Apache removes the directory prefix before matching. A file in the document root therefore normally uses ^docs/, not ^/docs/. A leading slash commonly appears in server or virtual-host examples, but copying it into per-directory configuration can prevent the rule from matching.

#1 Best Overall
Sale
Compressed Air Duster,130000RPM Super Power Cordless Air Duster,Rechargeable Brushless Blower,3 Gear Adjustable Blower with LED Light,Dust Remover for Computer/House/Outdoor/Car
  • Powerful Turbo Fan:CAGIWIRU Electric Air Duster have reached speed up to 130000RPM,Can efficient remove dust and debris.With LED light and Three Adjustable Speed to meet with different cleaning tasks.
  • With Attached USB C Fasting Charging cable.Charing 3 hours,Enjoy up to 240 minutes of use on the lowest setting, For third speed can use for 25mins.with four charging options to meet with your needs.
  • Wide Application:Upgraded attached 5 different nozzles,making it suitable for a variety of scenes, such as car,pet, pc, keyboards, and other electronic devices. It also serves for office and home clean duster.
  • Handy Design & Portable:Cordless air duster electric nozzle is Light weight and small size, design and comfortable to hold and space saving, convenient to carry around. You can put it in your room or inside the car without taking up space.
  • Warranty & Support: Our Electric air duster comes with 5 years warranty and 24/7 customer service. During this period, if you have any questions,pls kindly feel free to contact us.

Internal rewrite: keep the subdomain in the browser

An internal rewrite changes how Apache locates a resource without sending a redirect response. The visitor continues to see the original subdomain and path, while Apache serves content from another path under the same document root.

Basic subdomain-to-directory rule

RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.example.com$ [NC]
RewriteRule ^(.*)$ app/$1 [L]

Place this in the document-root .htaccess. A request for https://app.example.com/docs/page.html is internally mapped to app/docs/page.html. The address bar remains app.example.com/docs/page.html; no 3xx response is sent.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The host expression is anchored with ^ and $. Without those anchors, a value such as not-app.example.com could satisfy an overly broad expression. [NC] makes the comparison case-insensitive, which is generally appropriate for DNS hostnames.

Preventing a rewrite loop

A catch-all rule can process its own destination on a later per-directory pass. Add exclusions before the catch-all when the destination directory is physically present beneath the document root:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.example.com$ [NC]
RewriteRule ^app(?:/|$) - [END]
RewriteCond %{HTTP_HOST} ^app.example.com$ [NC]
RewriteRule ^(.*)$ app/$1 [END]

The first rule leaves an already-prefixed app path alone. The hyphen substitution means “no substitution.” [END], available in Apache 2.4, stops all per-directory rewrite processing for the current request. If your configuration must support an environment where only the traditional stop flag is appropriate, [L] stops the current rule set and allows a later per-directory pass to occur.

Another common safeguard is to leave existing files and directories untouched, but use that only when it matches your routing design:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
EVEO® Screen Cleaner Spray Kit (Streak-Free) TV Screen Cleaner for Smart TV, Lcd, LEd, Oled | Computer Screen Cleaner Spray and Wipe Microfiber Cloth - TV, Monitor, Laptop, iPad, Car - 6.7oz 1 Pack
  • Screen Cleaner is Your Screen’s New Bestfriend! - EVEO is proud to present a screen cleaner that’s perfectly suited for your TV. Meet your new screen cleaner, computer screen cleaner, laptop cleaner, iPad cleaner, Macbook,tv cleaner or any other electronic device. Our screen cleaner spray and wipe along with the included plush microfiber cloth, easily removes dust, fingerprints & other smudges on the screen’s surface. Clean effectively for a streak-free screen
  • Gentle on your Screens - Our screen cleaner spray and wipes is compatible to be used even for the most sensitive LCD, LED, CRT, and OLED screens in proper use. Be at ease knowing that this product is compatible with all the major brands: LG, SONY, Samsung, Sharp, iPad, iPhone, Android screens, Kindle, PC monitors - we’ve got you covered! Can be used as LCD screen cleaner, tv cleaner, smart tv screen cleaner, and more
  • Premium Super Soft Plush Microfiber Cloth - Included in this efficient screen cleaning kit. This screen cleaner spray and wipe has our signature plush microfiber cloth specially designed to comply with screens and monitors and leave them streak-free. Has been shown to effectively absorbs stubborn stains, zero streaks, and no fiber shedding. Always use this together with our screen cleaner for the best results
  • Easy-to-Use Screen Cleaner Kit + microfiber cleaning cloth - The EVEO Screen Cleaning Kit will easily be your new favorite tool when it comes to taking care of the things you love most, quick and easy solution for a streak-free screen. Simply screw on the sprayer gun on the screen cleaner bottle. Spray the cleaning liquid onto the microfiber cloth, then wipe the screen until desired cleanliness & shine is achieved.
  • Quality Screen Care Kit Worth Purchasing - Enjoy a convenient Screen Cleaner anytime, anywhere you need it. Your satisfaction is our top priority. We stand behind the quality of our products and that’s why, you shouldn’t be worrying at all. We manufacture the Best Screen Cleaner. and can be used for laptop cleaning kit, laptop screen cleaner, tv screen cleaner for smart tv. MacBook screen cleaner, monitor cleaner or screen cleaner spray for electronic devices with microfiber cleaning cloth
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.example.com$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteCond %{HTTP_HOST} ^app.example.com$ [NC]
RewriteRule ^(.*)$ app/$1 [END]

Here, an existing file or directory is served directly; other paths are mapped into app/. If your application intentionally handles every path, do not add this exception blindly.

External redirect: send the browser to another host

Use an external redirect when the visible URL must change. The substitution must be an absolute URL, and the response status tells clients and caches how to treat the move.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.example.com$ [NC]
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301,END,NE]

This sends requests from old.example.com to the corresponding path on www.example.com. The optional slash in ^/?(.*)$ makes the pattern tolerant of contexts that provide a slash, although a document-root .htaccess pattern normally receives no leading slash. The R=301 flag makes the move permanent; do not use 301 while you are still experimenting with the destination or ordering.

[NE] prevents mod_rewrite from escaping certain characters in the redirect result. It is useful only when you understand the encoding implications. For ordinary redirects, preserving normal URL escaping is safer, so omit [NE] unless a specific character-handling requirement justifies it.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Temporary testing before a permanent move

Start with a temporary status while validating the rule:

RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=302,END]

Check the response with a browser’s network panel or a command-line HTTP client, then change to 301 only after the host, path, HTTPS behavior and exclusions are correct. Browsers cache permanent redirects aggressively, which can make a fixed rule appear broken until you test with a fresh profile or clear the relevant cache.

Rank #3
BladeHawks RGB Gaming Mouse Pad Extra Large Extended 31.5x12 Inch-Black
  • 【Large size】:Measuring 800 x 300 mm/ 31. 5×12 inches, which is wider and taller than others. Large mouse pad can perfectly fit your keyboard and mouse of any size, and there is also ample space for peripherals such as phones, tablets, etc.
  • 【LED backlight mouse pad】:Blade Hawks RGB mouse pad provides 7 static modes and 7 dynamic modes will give you the options you are looking for. Power-off memory function remembers the last lighting mode you selected. The super glow fiber Chroma will give you a colorful gaming setup that you want.
  • 【Anti-slip rubber base】:Made from Natural rubber, this gaming mousepad will firmly grip your desktop, allowing you to enjoy the ultimate precision in the games you are most passionate about. It also comes with a texture that enhances this ability even further.
  • 【Smooth and waterproof surface】: Micro-textured cloth surface, for extremely precise mouse control and a smooth movement. When liquid splashes on the gaming mouse pad, it forms water droplets and slides down. Will not delay your work or gameplay.
  • 【Plug and play】: This mouse and keyboard pad is powered by USB, plug and play, no driver required. One button control light modes. Press one time to change the lighting mode, press twice to change the brightness, press and hold about 3 seconds turn ON/OFF.

Internal rewrite versus external redirect

Characteristic Internal rewrite External redirect
Browser address Stays on the requested host and path Changes to the URL in the Location header
Typical substitution Relative path such as app/$1 Absolute URL such as https://www.example.com/$1
Response Usually a normal 2xx response after internal routing 3xx response, such as 301 or 302
Host change No visible host change Can move the request to another host
SEO and caching implication The requested URL remains canonical from the client’s perspective A permanent redirect signals that the destination is the new URL; caches may retain it
Destination location Normally remains within the same Apache document root Can point to another virtual host, server or domain
Best use Serving a subdomain from a dedicated directory without changing its public URL Canonicalizing a host or permanently moving content

Choose one behavior deliberately. A rule that internally maps app.example.com into app/ should not also redirect that host to itself. Conversely, a host migration should not rely on an internal rewrite if visitors and search engines must discover the new URL.

Host matching details that prevent subtle failures

Account for a port during local testing

%{HTTP_HOST} can include a non-default port. A strict expression for production traffic may therefore fail against a local request such as app.example.com:8080. If you need both forms, make the port optional:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
RewriteCond %{HTTP_HOST} ^app.example.com(?::[0-9]+)?$ [NC]

Do not make the host condition broad merely to hide a port mismatch. Keep the hostname anchored and permit only the port forms your test environment requires.

Use condition and rule backreferences correctly

Captures in the preceding RewriteCond are referenced as %1, %2 and so on. Captures from the RewriteRule pattern are referenced as $1, $2 and so on. For example:

RewriteCond %{HTTP_HOST} ^([^.]+).example.com$ [NC]
RewriteRule ^(.*)$ tenants/%1/$1 [END]

For blue.example.com/docs, %1 is blue and $1 is docs. Keep the captures narrow; an accidental extra pair of parentheses can change which backreference is populated.

Preserve or replace query strings intentionally

Because the query string is not matched by RewriteRule, a path rewrite normally carries the existing query string forward. If the destination must receive a newly constructed query string, use the appropriate query-string flags and test both requests with and without parameters. Never assume a path capture has also captured ?key=value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
Vaydeer Wrist Rest for Keyboard and Mouse, Computer Ergonomic Wrist Support Pad, Soft Memory Foam Arm Cushion for Desk, Palm Hand Office Laptop Typing
  • 【Softer and More Comfortable】Vaydeer wrist rest has unique diamond pattern, which is the combination of softness and aesthetics. The materials of wrist rest are improved into higher quality memory foam and covered with silky smooth lycra. The computer wrist rest makes you as comfortable and cushiony as like rest your wrists on clouds.
  • 【Ergonomic Wrist Saver】The wrist rests for keyboard and mouse comes with a 17.32×3.15×0.83 inch keyboard wrist pad and a 5.94×3.15×0.83 inch mouse wrist support. Based on ergonomic design, the unique concave shape is the perfect fit for your wrist joints. The wrist rest pad fits most computer keyboards and laptops, improve hand and wrist posture, release your wrist and arm stress.
  • 【Non-Slip Rubber Bottom】Featuring an anti-skid silicone base on the bottom, this wrist keyboard support stays firmly in place on your desk, preventing the padding from sliding around, ensuring stable and consistent wrist support during extended computer sessions.
  • 【Better Experience & Pain Relief】Our keyboard arm rest is beneficial to alleviate the soreness caused by direct contact and friction between your arm and a hard desk surface, reducing the risk of wrist fatigue or carpal tunnel. The soft texture of memory foam can evenly distribute the pressure around your wrists and provide good support with just enough give.
  • 【Helpful in Multiple Scenarios】Whether you're working, studying, writing, typing, gaming, this keyboard and mouse rest combo is an essential accessory to add comfort and support to your hands and wrists. It’s also a great gift for men, women, family, friend, coworker, gamer, teacher, etc.

Choosing .htaccess or virtual-host configuration

Factor .htaccess VirtualHost/server configuration
Administrative access Useful when you control a site directory but not the main Apache configuration Requires access to the server or virtual-host configuration
Scope Applies to the directory and its descendants; location changes the path seen by the rule Can apply cleanly to an entire virtual host
Performance Apache checks per-directory configuration during requests Configuration is loaded centrally and avoids per-directory lookup overhead
Maintainability Convenient for application-level deployment, but rules can be scattered Centralized and easier to audit when an administrator owns the host
Canonical host redirect Possible with RewriteCond and RewriteRule A dedicated Redirect in a <VirtualHost> is the cleaner choice when server access exists

Apache’s remapping guidance recommends a dedicated virtual-host Redirect when that configuration access is available. Use .htaccess when deployment constraints require it, and document which directory contains the file.

Prerequisites before debugging the expression

  • DNS: The subdomain must resolve to the Apache server that will process the request.
  • Virtual host: The request must arrive at the intended virtual host and document root. A perfect rule in the wrong virtual host will never produce the expected result.
  • Module: mod_rewrite must be loaded, and the file must contain RewriteEngine On.
  • Override policy: The directory configuration must permit rewrite directives in .htaccess. An override policy that disallows them can produce a 500 error or make the file ineffective.
  • Correct location: A document-root file and a file in a subdirectory do not see the same path. Apache strips the prefix for the directory containing the file.
  • Target exists: For an internal mapping, verify that the destination directory and application entry point are readable by Apache.

A safe deployment procedure

  1. Confirm DNS resolution and identify the virtual host handling the subdomain.
  2. Place a minimal rule in the correct .htaccess, beginning with RewriteEngine On.
  3. Use an anchored host condition and a narrow test path instead of enabling a broad catch-all immediately.
  4. For a routing test, use an internal relative substitution and verify that the browser URL remains unchanged.
  5. For a host move, use a temporary redirect such as 302 and inspect the returned Location header.
  6. Test the root path, a nested path, a nonexistent path, an existing static file, and a request with a query string.
  7. Check both the intended subdomain and a neighboring hostname to ensure the condition is not overbroad.
  8. Review the Apache error log after each change, especially if the server returns 500.
  9. Only after behavior is correct, add exclusions, choose [L] or [END] deliberately, and change a permanent move to 301 if that is truly intended.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting common symptoms

The rule never matches the subdomain

The usual cause is putting the hostname in RewriteRule. Move that test to RewriteCond %{HTTP_HOST}. Then verify DNS, the selected virtual host and the document-root location of the file.

A pattern copied from another example fails

Remove the leading slash when the rule is in .htaccess. Test ^docs/ rather than ^/docs/. A server-context rule and a per-directory rule receive different path input.

The server returns 500

Read the Apache error log first. Confirm that mod_rewrite is loaded, the directive is allowed by the directory’s override policy, brackets and flags are valid, and the substitution does not contain a malformed escape. A configuration error is more likely than a hostname mismatch when Apache returns 500.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The internal rewrite loops

Inspect whether the destination path also satisfies the catch-all rule. Add a destination exclusion before the broad rule, or stop processing with [END] where Apache 2.4 is available. Also check for another .htaccess or application-level redirect that sends the request back into the same mapping.

The redirect loops

Ensure the destination host no longer satisfies the redirect condition. A rule redirecting to a host that still matches the same expression will repeat indefinitely. Check the order of HTTP-to-HTTPS and host-canonicalization rules as well; each should have a clear condition and a destination that does not trigger the previous rule.

Best Value
Sale
Under Desk Cable Management Tray No Drill, Metal Mesh Cable Management Under Desk with Clamp Mount, 2 Hole Cable Tray with Wire Management and Cord Organizer for Home Office Standing Desk
  • 【No Drill Mounted】Cable management tray can be assembled in as little as 3 minutes or less-simply. Side clips easily clampe the edge of desk, pass through the holes on the sides, and secure excess cables with baskets. Desk cable management supports inward or outward installation to meet your needs in different scenarios, and provides you with great convenience when collecting and organizing wires.
  • 【Rubber Pad Protects Desk】Wire organizer under desk built-in soft rubber pads on the clamp protect your desk from scratches, so you will not damage your furniture or office.
  • 【Sturdy and Beautiful】Cord management under desk is made of high-quality carbon steel and holds about 15 lbs. The wire basket has 2 holes on each side of the tray to enable your cables to pass freely. Cable baskets are suitable for desk thickness use from 0.4" to 2.4".
  • 【Space Saving & Keep Organized】Desk cable management tray holds all your power cords, outlet strips, USB hubs and computer transformers hidden from your feet, and the data cables will not enter the vacuum cleaner to keep your desk clean and tidy. Desk wire organizer is also suitable for use in the kitchen and outdoors.
  • 【Reduce Safety Risks】Cable tray under desk keeps all plugs away from your kids, no more worry about tripping. Using 2 holes wire tray saves extra space, helps you easily cabling and wire protection. The mesh design is not easy to accumulate dust, but avoids safety accidents caused by messy wires.

The redirect points to the wrong path

Inspect the capture boundaries. The rule’s $1 comes from the path pattern, while a condition’s capture uses %1. Confirm whether the file is in the document root or a subdirectory and whether the substitution intentionally includes a trailing slash.

Only some hostnames are affected

Check case handling, optional ports and anchoring. [NC] handles case differences; an optional numeric port handles local testing. Do not replace an anchored hostname with a substring match, because that can catch unrelated subdomains.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Flags worth understanding

  • [L]: Stops processing the current rewrite rule set. Apache documents it as causing mod_rewrite to stop processing the rule set.
  • [END]: Stops all per-directory rewrite processing for the current request. It is useful for preventing another .htaccess pass from re-entering a completed mapping and requires Apache 2.4.
  • [R=301] or [R=302]: Sends an external redirect with the selected status. Use 301 only for a permanent move.
  • [NC]: Makes a condition or rule comparison case-insensitive; use it for hostnames when appropriate.
  • [NE]: Suppresses normal escaping in a redirect. Apply it only when you understand the resulting URL encoding.

Reusable patterns

One subdomain mapped to one directory

RewriteEngine On
RewriteCond %{HTTP_HOST} ^docs.example.com$ [NC]
RewriteRule ^(.*)$ documentation/$1 [END]

This keeps docs.example.com visible while serving files beneath documentation/.

Several subdomains captured dynamically

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com$ [NC]
RewriteRule ^(.*)$ sites/%1/$1 [END]

This pattern maps each permitted label into a matching directory. Restrict the condition to the labels your application actually supports; otherwise arbitrary hostnames could select unintended directories.

Redirect an old subdomain while retaining the path

RewriteEngine On
RewriteCond %{HTTP_HOST} ^legacy.example.com$ [NC]
RewriteRule ^/?(.*)$ https://new.example.com/$1 [R=301,END]

Use this only after validating the new host and all path behavior with a temporary status.

What to record for future maintenance

  • The exact .htaccess location and the document root it belongs to.
  • The hostnames and optional ports the conditions intentionally accept.
  • Whether each mapping is internal or external and, for redirects, whether the status is temporary or permanent.
  • The expected destination directory and any exclusions placed before a catch-all.
  • The Apache version, especially when relying on [END].
  • A small test matrix covering host, path, query string, HTTPS and existing files.

Clear comments near each rule should explain the intended host and destination, not merely repeat the syntax. This makes later HTTP-to-HTTPS or canonical-host changes less likely to create loops.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.