Redirect to another folder

Deadly 提交于 2019-12-20 07:14:08

问题


I read this .htaccess rewrite to redirect root URL to subdirectory and i'm trying to achieve the same things.

The solution with most up votes was:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]

Now, looking at the comments it seems to be working good. The problem is that I'd like not to hardcode any path (as instead provided above "www.example.com"). I'd like that my .htaccess inside projectname/ redirects to projectname/public/ despite what the real server host is. So that if I put projectname/ inside the root server of www.pinco.com it redirects to www.pinco.com/projectname/public/ but it shows www.pinco.com/projectname/.

How can I achieve that?


回答1:


The example you found is actually doing two different things.

# This is actually just redirecting all http://example.com/somepage/
# to http://www.example.com/somepage/, to ensure all URLs have the www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]  

The second rewrite is what will help you achieve what you're trying to do.

# This redirect all requests for http://example.com -> http://example.com/newdir
# If you are looking to redirect the request, so the URL contains directory name,
# you can change the [L] to [R=301,L]
RewriteRule ^$ /newdir [L]

# If your concerned about direct access to a particular page without the sub-dir 
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/newdir
RewriteRule (.*) /newdir$1 [R=301,L]

So, in that case, you won't have to be concerned with domain that the application is running on.




回答2:


try to add this line, and :

RewriteBase /projectname

And you stay inside the project.



来源:https://stackoverflow.com/questions/5477008/redirect-to-another-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!