.htaccess配置求助:重定向父路径到子目录并保留原URL,保留子路径访问
Hey there! Let's get this .htaccess sorted out for you. The issue you're hitting is because the R flag triggers an external redirect, which changes the URL in the browser. What you need is an internal rewrite—this serves the content from your target path while keeping the original URL intact.
Here's the adjusted .htaccess configuration that meets both your requirements:
# Enable mod_rewrite RewriteEngine On # Skip the rewrite rule if the request is for a real file or directory # This ensures /services/service1/2/3 and other actual paths work normally RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Internally rewrite /services (with or without trailing slash) to your target page # No R flag = internal rewrite, so the URL stays as /services RewriteRule ^services/?$ /services/doc/index.html [L]
Let me break down what each part does:
RewriteEngine On: Turns on Apache's rewrite module (make sure mod_rewrite is enabled on your server first!).- The two
RewriteCondlines: These check if the requested path isn't a real file (!-f) or directory (!-d). This is crucial because it lets users still access actual paths like/services/service1/2/3without interference. RewriteRule ^services/?$ ...: The pattern^services/?$matches exactly/servicesor/services/(the?makes the trailing slash optional). The[L]flag tells Apache to stop processing further rules once this one matches—no need to run anything else after this.
Test this out:
- Visit
myhost.com/services(ormyhost.com/services/)—your browser URL will stay the same, but you'll see the content from/services/doc/index.html. - Visit
myhost.com/services/service1/2/3—it should load the actual service page like normal.
内容的提问来源于stack exchange,提问作者user8215181
相关产品推荐
相关产品推荐

