2010-05-04 8 views
12

Lưu ý: Tôi đã thử các đề xuất dưới this question nhưng chúng không hoạt động hoặc cho tôi lỗi lặp vô hạn.Sử dụng Mod_Rewrite để ghi ROOT sang đường dẫn khác

Tôi có sau trong tập tin .htaccess của tôi:

<IfModule mod_rewrite.c> 

    Options +FollowSymLinks 
    RewriteEngine On 
    RewriteBase/

    # Rewrite old links to new 
    Redirect 301 /howitworks.php  /how-it-works 
    Redirect 301 /testimonials.php  /testimonials 
    Redirect 301 /contact_us.php  /customer-service 
    Redirect 301 /affiliates.php  /affiliates 
    Redirect 301 /account.php   /customer/account 
    Redirect 301 /shopping_cart.php  /checkout/cart 
    Redirect 301 /products.php   /starter-kits 
    Redirect 301 /login.php    /customer/account/login 

    # Force to Admin if trying to access admin 
    RewriteCond %{HTTP_HOST} www\.example\.com [NC] 
    RewriteCond %{REQUEST_URI} admin [NC] 
    RewriteRule ^(.*)$ https://admin.example.com/index.php/admin [R=301,L,QSA] 

    # Compress, Combine and Cache Javascript/CSS 
    RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d=$2 

    # Always send 404 on missing files in these folders 
    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/ 

    # Never rewrite for existing files, directories and links 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-l 
    RewriteCond %{REQUEST_URI} !server-status 

    # Rewrite everything else to index.php 
    RewriteRule .* index.php [L] 

    # Force www in url and non-example domains to example 
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^ww2\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^qa\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^uat\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^local\.example\.com [NC] 
    RewriteCond %{HTTP_HOST} !^admin\.example\.com [NC] 
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA] 

</IfModule> 

Tôi cần một viết lại rằng sẽ làm như sau:

Rewrite: http://subdomain.example.com/đểhttp://subdomain.example.com/page

Tôi cũng muốn điều này để ẩn/trang (/ sẽ hành động như thể nó là/trang) nhưng đây không phải là một yêu cầu cần thiết. Ngoài ra, tôi muốn nó hoạt động với HTTP hoặc HTTPS.

Bất kỳ trợ giúp nào được đánh giá cao vì tôi đã phải vật lộn với điều này hàng giờ liền.

+1

Câu hỏi này thuộc về ServerFault. – leek

Trả lời

20

Một chuyển hướng đúng đắn về đường dẫn gốc chỉ đơn giản là:

RewriteEngine On 
RewriteRule ^/$ /page [R] 

để lực lượng này chỉ trên một tên miền phụ (mà chỉ nên xảy ra nếu khối viết lại của bạn là trong một định nghĩa virtualhost):

RewriteEngine on 
RewriteCond %{HTTP_HOST} =subdomain.example.com 
RewriteRule ^/$ /page [R] 
+5

Câu trả lời này không hoạt động trong ngữ cảnh htaccess, và chuyển hướng đầy đủ cho khách hàng (IOW không "ẩn" như được yêu cầu) – covener

+4

Điều này chỉ làm việc cho tôi trên apache2 khi tôi sử dụng ^/$ thay vì^$. Câu trả lời có đúng không hoặc tôi có mắc sai lầm không? –

+0

Sai: RewriteRule^$/page [R] Phải: RewriteRule ^/$/page [R] – Hartmut

5

Sao chép phản hồi đã xóa của tôi:

Trong htaccess của tài liệu của bạn chỉ cần:

RewriteEngine on 
RewriteCond %{HTTP_HOST} =subdomain.example.com 
RewriteRule ^$ /page 
+1

Không chắc tại sao lại bị xóa ... 'RewriteRule^$/page' có vẻ là câu trả lời hợp lệ duy nhất. 'RewriteRule ^/$/page' không _NOT_ khớp với yêu cầu tới gốc tài liệu của trang web.Tôi đang sửa đổi câu trả lời hiện được chấp nhận để câu trả lời chính xác. – joshperry

1

Bạn có 2 lựa chọn để viết lại gốc đối với con đường khác,

1) DirectoryIndex

DirectoryIndex anotherPath 

2) Mod-rewrite:

RewriteEngine on 


RewriteRule ^/?$ /anotherPath [L] 

Bạn có thể sử dụng một trong trong số các tùy chọn ở trên để chuyển hướng nội bộ example.com/ đến example.com/anotherPath.

refrences:

https://httpd.apache.org/docs/current/mod/mod_dir.html

http://httpd.apache.org/docs/current/mod/mod_rewrite.html