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

To keep https://example.com/about in the browser while Apache serves the files from a physical /public directory, use an internal rewrite in the document-root .htaccess file. If the browser should change to https://example.com/public/about, use an external redirect instead. The two configurations are different, and confusing them is the most common cause of unwanted URL changes and rewrite loops.

Choose between an internal rewrite and an external redirect

A domain-to-subfolder setup can produce two completely different results. Decide which URL visitors should see before editing .htaccess.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Goal Browser address Apache response Typical use
Internal rewrite Remains https://example.com/path Apache maps the request to /public/path internally; no 3xx response is sent Keep the folder name private while the application lives in a subdirectory
External redirect Changes to https://example.com/public/path Apache sends a 3xx response with a Location header Make the subfolder part of the public, canonical URL

A path substitution without the R flag is an internal rewrite. A fully qualified destination URL, or a substitution using R, is an external redirect. Use a temporary redirect while testing and change it to a permanent status only after the behavior is confirmed.

Prerequisites and file location

The examples assume that:

  • DNS for example.com already points to this Apache site.
  • The real website files are in a physical directory named public beneath the document root.
  • The .htaccess file is located directly in the document root, not inside public.
  • Apache’s mod_rewrite module is enabled.
  • The virtual host permits overrides for this directory. A server administrator controls this through the Apache override settings.

For example, if the document root is /var/www/example, the relevant paths are:

#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.
/var/www/example/.htaccess
/var/www/example/public/index.html
/var/www/example/public/about/index.html

In .htaccess and other per-directory contexts, Apache removes the directory prefix before matching a RewriteRule. Therefore, a rule in the document-root file normally matches about, not /about. This differs from a rule written in the main virtual-host configuration. Apache’s documentation describes this per-directory behavior and the separate roles of RewriteRule and RewriteCond.

Hide the subfolder with an internal rewrite

Place this configuration in the document-root .htaccess file:

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

# Apply only to the intended host.
RewriteCond %{HTTP_HOST} ^(?:www.)?example.com$ [NC]

# Do not prepend /public a second time.
RewriteCond %{REQUEST_URI} !^/public(?:/|$) [NC]

# In document-root .htaccess, the RewriteRule pattern is relative.
RewriteRule ^ public%{REQUEST_URI} [END]

Replace example.com with the hostname you actually intend to serve. Keep the backslashes in the regular expression. The condition accepts both the bare hostname and its www form, without accepting arbitrary host names.

How the internal rule processes a request

  1. RewriteEngine On enables rewrite processing for this directory.
  2. The HTTP_HOST condition limits the rule to example.com and www.example.com. The [NC] flag makes the hostname comparison case-insensitive.
  3. The REQUEST_URI condition checks the original path. A request already beginning with /public is excluded, so Apache does not keep adding another public segment during per-directory processing.
  4. The rule pattern ^ matches the relative path supplied to the root .htaccess context. The substitution joins public to the original URI. A request for /about is therefore served from /public/about, while the client still knows only about /about.
  5. [END] stops further per-directory rewrite processing for the request on Apache versions and hosting environments that support it. If your environment does not support [END], use [L] and retain the destination-prefix exclusion. Verify the exact Apache version and test the result because [L] and [END] do not have identical processing scope.

What visitors see

With the internal configuration, these requests keep their original browser paths:

Requested URL File or directory Apache serves Visible URL
https://example.com/ /public/, usually resolved through its directory index https://example.com/
https://example.com/about /public/about https://example.com/about
https://example.com/assets/site.css /public/assets/site.css https://example.com/assets/site.css

If the requested path does not exist under public, Apache’s normal handling determines the result. Depending on the files and other rules in the application, that may be a 404, an index-file fallback, or an application-generated response. The rewrite itself does not guarantee that every path has content.

Show the subfolder with an external redirect

Use this version when the public URL must include /public:

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} ^(?:www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/public(?:/|$) [NC]
RewriteRule ^ https://example.com/public%{REQUEST_URI} [R=302,END,NE]

A request for /about receives a temporary redirect to https://example.com/public/about. The browser then makes a second request to that new URL. The exclusion condition is still required: without it, the redirected request would match again and could become a loop such as /public/public/about.

Why testing should use 302 first

R=302 tells clients and intermediaries that the redirect is temporary. It is safer while you check both host variants, HTTP and HTTPS, existing files, missing paths, query strings, and any other canonicalization rules. Browsers and caches can retain a permanent redirect for a long time, making a mistake harder to observe after the fact.

After the destination and canonical-host behavior are correct, replace R=302 with R=301 only if the move is genuinely permanent:

RewriteRule ^ https://example.com/public%{REQUEST_URI} [R=301,END,NE]

Do not use a permanent status merely to make a test appear successful. Clear cached permanent redirects, or test with a client that does not reuse its cache, whenever you change a 301 during troubleshooting.

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

Query strings and the redirect target

The examples operate on the path in REQUEST_URI. Apache normally carries the existing query string forward when the substitution does not supply a new one. For example, a request such as /search?q=apache should redirect or rewrite with the ?q=apache portion preserved under the normal mod_rewrite rules. Test the exact query strings used by your application, especially if other rewrite rules add, remove, or replace parameters.

Understand matching, directories, and rule order

REQUEST_URI includes the leading slash, which is why the exclusion tests for /public. The RewriteRule pattern in the root .htaccess file does not normally include that slash because Apache has already removed the per-directory prefix. Mixing those two forms in one expression is a frequent source of rules that appear correct but never match.

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.

The rule is intentionally broad: ^ covers the root request, nested pages, and static assets. The substitution then adds the physical directory once. If you need a narrower mapping, replace the broad pattern with a relative path pattern, but keep the same host condition and destination-prefix guard.

Rule order matters when the file already contains application rewrites. Apache evaluates rules in sequence. A rule placed above the folder mapping may redirect or rewrite the request first; a rule placed below it may never see the original path, particularly when processing is terminated. Review existing rules before inserting this block, and make sure the intended host condition applies to the whole mapping.

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

The exclusion also means that a request already written as /public/about is not mapped to /public/public/about. It does not, by itself, remove the physical directory from direct requests or change links generated by the application. It only prevents this mapping rule from adding the prefix again.

Why common RewriteRule attempts fail

A leading slash in a root .htaccess pattern

This rule is commonly copied from a virtual-host example:

RewriteRule ^/about public/about [L]

In a document-root .htaccess context, Apache has already removed the directory prefix and normally presents the path without its leading slash. Start the pattern with ^, or match the relative segment you need:

RewriteRule ^about$ public/about [L]

The exact pattern depends on the paths you want to handle, but the per-directory rule must not assume the same leading-slash behavior as server configuration.

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.

The hostname is placed in RewriteRule

RewriteRule matches the URL path, not the hostname. A pattern such as ^example.com will not test which domain reached the server. Put the host test in a RewriteCond that reads %{HTTP_HOST}, as in the working configurations above.

The rule loops

A loop occurs when the rewritten destination is processed again and the same prefix is added repeatedly. The essential guard is:

RewriteCond %{REQUEST_URI} !^/public(?:/|$) [NC]

The boundary expression matters. It excludes /public and paths below it, but not unrelated names such as /publication. Keep this test before the rule that prepends the directory.

The wrong .htaccess file is edited

A rule in the document-root file runs in a different per-directory context from a rule inside /public/.htaccess. If you move the file, the relative path seen by RewriteRule changes. Start by confirming the absolute filesystem location of the active document root and the location of every .htaccess file involved.

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

Apache ignores the configuration

If the request behaves exactly as though the file does not exist, possible causes include:

  • The host does not allow overrides for the document root.
  • mod_rewrite is not loaded.
  • The request is reaching a different virtual host than the one you edited.
  • Another configuration layer, application rule, or proxy is handling the request first.
  • The file name is wrong, permissions prevent Apache from reading it, or a syntax error causes the configuration to be rejected.

Check the virtual-host configuration and Apache error logs with the site administrator. A syntax or permission problem is more useful to diagnose from the server log than from the browser’s generic error page.

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.

HTTP and HTTPS rules fight each other

It is common to combine a folder mapping with an HTTP-to-HTTPS redirect and a bare-host-to-www redirect. If each rule makes a separate assumption about the current host or scheme, requests can bounce between destinations. Test the combinations deliberately and ensure that each canonicalization rule has one clear target.

When TLS terminates at a reverse proxy, Apache may receive an HTTP connection even though the visitor used HTTPS. Before adding a scheme condition, confirm which request variable reflects the original client scheme in your proxy setup. Do not blindly stack an HTTPS rule on top of the folder rule.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Host validation and redirect security

Never construct a redirect destination from an unvalidated host supplied by the client. A request can contain an unexpected Host header; reflecting that value into a fully qualified redirect can create an open-redirect vulnerability or send users to an attacker-controlled domain.

The fixed destination in the example, https://example.com, avoids that problem. The allowlisted HTTP_HOST condition also ensures that the rule is not applied to every domain sharing the server. If you serve several legitimate domains, give each one an explicit condition and fixed destination rather than copying arbitrary input into the redirect URL.

Testing the mapping before going live

Use an HTTP client such as curl to inspect status codes and headers rather than relying only on a browser, which may cache redirects. Run tests against the actual host names:

curl -I http://example.com/
curl -I https://example.com/
curl -I http://www.example.com/
curl -I https://www.example.com/
curl -I https://example.com/about
curl -I https://example.com/assets/site.css
curl -I https://example.com/missing-path
curl -I 'https://example.com/search?q=apache'
curl -I https://example.com/public/about

For an internal rewrite, the response should not expose a redirect to /public; the browser URL remains unchanged while Apache serves the mapped content. For an external test configuration, inspect the 302 response and its Location header. Once the destination is confirmed, repeat the checks after changing to 301 if the move is permanent.

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

Verification checklist

  • The root URL serves the intended index from public.
  • A nested route maps to the corresponding path under public.
  • An existing static file loads without an extra directory segment.
  • A missing path produces the intended application or server response.
  • Both the bare host and the www host behave according to your canonical-host policy.
  • HTTP and HTTPS requests do not bounce between rules.
  • Query strings remain available to the application when they should.
  • A request already beginning with /public is not prefixed again.
  • The external version returns the intended 3xx status and Location header; the internal version does not return a redirect merely to serve the folder.

When a virtual-host configuration is better

If you administer Apache’s main configuration, a dedicated virtual-host rule is usually preferable for a simple host move. Apache’s remapping guidance favors a Redirect directive in the virtual host for straightforward redirects because it avoids per-directory processing and makes the host-level intent explicit.

Use .htaccess when you do not control the main configuration or your hosting provider specifically requires directory-level overrides. Remember that a configuration copied between contexts may need a different pattern, a different substitution form, or different processing flags. Always evaluate the rule in the context where it will actually run.

Practical deployment sequence

  1. Confirm DNS, the active virtual host, the document root, and the physical public directory.
  2. Decide whether the folder should remain hidden or become part of the public URL.
  3. Back up the current .htaccess file and note any existing application rewrites.
  4. Add the host condition and destination-prefix exclusion before the mapping rule.
  5. Use the internal configuration for a hidden folder, or the external configuration with R=302 for a visible folder.
  6. Run the root, nested-path, asset, missing-path, host-variant, scheme, query-string, and already-prefixed tests.
  7. Review Apache’s error log if the file is ignored, a 500 error appears, or another rule wins.
  8. Only after successful testing, change a truly permanent external move to R=301.
  9. If you later change a permanent destination, account for cached 301 responses when validating the new behavior.

The durable principles are simple: test the hostname in RewriteCond, match the relative path in a per-directory RewriteRule, exclude the destination prefix to prevent loops, and choose an internal substitution or an explicit 3xx redirect based on the URL visitors should see.

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.

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