htaccess and rewriting

2019-03-02 13:37发布

I just made a subdomain on my webside mainly becouse i want a spesific address to it.

Lets say my website is : http://website.com And my subdomain is http://sub.website.com with its main folder in /home/username/public_html/sub/

When i enter the subdomain address the address suddently changes to http//website.com/sub/

how can i keep the subdomain address?

EDIT for Dennis:

i have a rule that directs all http://www.website.com to http://website.com

I use

RewriteEngine on
Redirect to non-www
RewriteCond %{HTTP_HOST} !^(website\.com)?$
RewriteRule (.*) http://website.com/$1 [R=301,L]

even if i comment out this the address still changes to

http://website.com/sub/

Best of regards, Alexander

2条回答
趁早两清
2楼-- · 2019-03-02 14:16

I would imagine something like this would work (not tested):

Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^website\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule \?cat_id=([0-9]+)&photo_id=([0-9]+) http://website.com/$1/$2/
RewriteRule ^/(.*)/(.*)/ /%1.php?cat_id=$1&photo_id=$2 [P]

Note: according to the documentation, Options FollowSymLinks needs to be an option you can override to get the rewrite to work.

The first RewriteCond will ensure the "webiste.com" host is not rewritten (but domains like sub.website.com will). The second one matches anything up to the first dot of the hostname. Both of these must succeed and if so the host part is accessible in %1.

The first RewriteRule rewrites the args to the php script to the directory paths (a change seen on URL on the browser). The next RewriteRule uses the [P] flag to proxy any requests of the form /xx/yy/ to %1.php (our remembered host name) with the args passed to the php script.

As I've stated, this is not tested, but hopefully it will get you "in the right direction".

EDIT: removed the [R] from the first rewrite rule.

查看更多
Bombasti
3楼-- · 2019-03-02 14:21

Another solution is to have a Virtualhost dedicated for your subdomain (that should already be the case, if not you'll get a lot of problems) and to make this VirtualHost ignore .htaccess instructions of the master domain. You should really try to keep your subdomain configuration independant of the master domain domain configuration.

Let's say your documentRoot in the subdomain VH is /home/username/public_html/sub/, when Apache serve the index.php or index.html file or anything else in this directoy it performs a recursive search of all .htaccess in /home/username/public_html/sub/, /home/username/public_html/, /home/username/, /home/ and /. So the rules defined in your master domain (I think it's in /home/username/public_html/) are applied.

You can tell this subdomain VirtualHost to ignore all .htaccess files which are before the VH DocumentRoot by adding:

<Directory />
   AllowOverride None
</Directory>
<Directory /home/username/public_html/sub/>
   AllowOverride All
</Directory>

You could even remove the AllowOverride All, remove all .htaccess and put the things you have in the .htaccess of your subdomain (if you have one) directly in the <Directory /home/username/public_html/sub/> section, same effect without having Apache searching for configuration parts on the filesystem (so faster).

查看更多
登录 后发表回答