Cấu hình Rewrite URL trên IIS7.5 Plesk Windows Hosting

Thảo luận trong 'Windows Hosting' bắt đầu bởi Nguyễn Thanh, 31/5/12.

  1. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    Để cấu hình rewrite url cho website của bạn.
    Trên linux ta có mod rewrite sử dụng file dạng .htaccess .Trên Plesk Windows Hosting của TND cũng có cơ chế .htaccess nhưng sẽ tốt hơn là dùng Mod Rewrite chuyên dụng của IIS 7.5 sử dụng file dạng web.config thường file này được đính kèm trong bộ cài đặt source rất dễ dàng tìm thấy.

    ở đây ví dụ tôi có http://forum.tnd.vn sử dụng mã nguồn xenforo. Tôi xóa files .htaccess ở thư mục root và thay vào đó là một files tên là web.config với nội dung như sau sẽ được kết quả tương tự và tối ưu hơn rất nhiều. Và tất nhiên hệ thống cũng sẽ dễ hiểu hơn.

    Xenforo
    Mã:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Imported Rule 1" stopProcessing="true">
                        <match url="^.*$" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
                        </conditions>
                        <action type="None" />
                    </rule>
                    <rule name="Imported Rule 2" stopProcessing="true">
                        <match url="^(data|js|styles|install)" />
                        <action type="None" />
                    </rule>
                    <rule name="Imported Rule 3" stopProcessing="true">
                        <match url="^.*$" />
                        <action type="Rewrite" url="index.php" />
                    </rule>
                </rules>
            </rewrite>
            <httpErrors existingResponse="PassThrough" />
        </system.webServer>
    </configuration>
    vBulletin 4 dành cho cả bản forum lẫn suite
    Mã:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- vBulletin 4 : IIS7 Rules -->
    <configuration>
        <system.webServer>
            <directoryBrowse enabled="true" />
            <rewrite>
                <rules>
                    <!-- Threads -->
                    <rule name="vB Threads" enabled="true" stopProcessing="true">
                        <match url="^threads/.*" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="showthread.php" appendQueryString="true" />
                    </rule>
     
                    <!-- Forums -->
                    <rule name="vB Forums" enabled="true" stopProcessing="true">
                        <match url="^forums/.*" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="forumdisplay.php" appendQueryString="true" />
                    </rule>
     
                    <!-- Members -->
                    <rule name="vB Members" enabled="true" stopProcessing="true">
                        <match url="^members/.*" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="member.php" appendQueryString="true" />
                    </rule>
     
                    <!-- Blogs -->
                    <rule name="vB Blogs" enabled="true" stopProcessing="true">
                        <match url="^blogs/.*" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="blog.php" appendQueryString="true" />
                    </rule>
     
                    <!-- Blog Entries -->
                    <rule name="vB Entries" enabled="true" stopProcessing="true">
                        <match url="^entries/.*" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="entry.php" appendQueryString="true" />
                    </rule>
     
                    <!-- CMS Rewrite-->
                    <rule name="vB CMS" enabled="true" stopProcessing="false">
                        <match url="^(?:(.*?)(?:/|$))(.*|$)$" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.php?r={R:2}" appendQueryString="true" />
                    </rule>
     
                    <!-- Final Check -->
                    <rule name="vB Final" enabled="true" stopProcessing="true">
                        <match url="^(.*)$" ignoreCase="true" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
                        </conditions>
                        <action type="CustomResponse" statusCode="404" statusReason="Not Found" appendQueryString="false" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    
    Nếu bạn sử dụng VBB4 Mod Rewrite Friendly URLs với cấu hình web.config như trên không chạy được thì bạn chạy code web.config như sau:

    Mã:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <directoryBrowse enabled="true" />
            <rewrite>
                <rules>
      <rule name="vB Threads" enabled="true" >
                        <match url="^threads/([0-9]+)(?:/?$|(?:-[^/]+))(?:/?$|(?:/p([0-9]+)?))" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="showthread.php?t={R:1}" />
                    </rule>
      <rule name="vB Forums" enabled="true" >
                        <match url="^forums/([0-9]+)(?:/?$|(?:-[^/]+))(?:/?$|(?:/p([0-9]+)?))" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="forumdisplay.php?f={R:1}" />
                    </rule>
      <rule name="vB Members" enabled="true" >
                        <match url="^members/([0-9]+)" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="member.php?u={R:1}" />
                    </rule>
      <rule name="vB Blogs" enabled="true" >
                        <match url="^blogs/([0-9]+)(?:/?$|(?:-[^/]+))(?:/?$|(?:/p([0-9]+)?))" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="blog.php?u={R:1}" />
                    </rule>
                    <rule name="vB Blog Entries" enabled="true" >
                        <match url="^entries/([0-9]+)(?:/?$|(?:-[^/]+))(?:/?$|(?:/p([0-9]+)?))" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="entry.php?b={R:1}" />
                    </rule>
      <rule name="vBCMS" stopProcessing="true" enabled="true" >
                        <match url="^(?:(.*?)(?:/|$))(.*|$)$" />
      <conditions logicalGrouping="MatchAll">
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                        <action type="Rewrite" url="{R:1}.php?r={R:2}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
     
    Coffee thích bài này.
  2. vitti

    vitti Thành Viên Mới

    anh ơi vậy nếu là phpbb thì em phải cấu hình web.config như nào ạ? em chạy mà không rewrite url được và bị lỗi trên host tnd. mong anh giúp đỡ. sang host khác thì vẫn ok.
     
  3. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    có sẵn files web.config trong gói cài đó bạn.
     
  4. vitti

    vitti Thành Viên Mới

    nhưng anh ơi mỗi host và mỗi domain phải có 1 htaccess riêng mà htaccess tương đương với web.config thì làm sao dùng cái mặc định trong gói cài được ạ :(
     
  5. vitti

    vitti Thành Viên Mới

    anh giúp em với forum phpbb em chạy restore trên host khác thì nó ok như này: http://vnit.mobi còn chạy trên host tnd thì bị như này ạ: http://zing4u.org :(
     
  6. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    Dùng 1 trong 2 cái thôi em, một là .htaccess 2 là web.config chỉ cần up lên root là được rồi.
     
  7. Bigbabol

    Bigbabol Thành Viên Mới

    Anh ui. Em có dùng source CMS Pro trên codecanyon cho website lớp em. Cài đặt trên localhost thì rất ok, nhưng khi chuyển lên host plesk của mình thì nó báo module_rewrite chưa được kích hoạt. Em có mài mò vài hôm nhưng vẫn chưa đc. Hì. Anh có thể giúp em không ạ? Tại em đầu tư nhiều về cái source đó lắm. Giờ ko dùng nó cũng tiếc. Hix
    Đây là code file .htacess có trong source đó.

    HTML:
    # Options:
    # -MultiViews: Turns off multiviews so it doesn't interfer with our rewrite rules
    # -Indexes: Stop directory listings
    # +FollowSymlinks: Let out rewrite rules work
     
    Options -MultiViews -Indexes +FollowSymlinks
     
    <IfModule mod_security.c>
        # Turn off mod_security filtering.
        # SecFilterEngine Off
     
        # The below probably isn't needed, but better safe than sorry.
        SecFilterScanPOST Off
    </IfModule>
     
    ErrorDocument 404 /cms/404.php
     
    <IfModule mod_php5.c>
        php_value register_globals 0
        php_value magic_quotes_gpc 0
        php_value session.auto_start 0
        php_value safe_mode 0
    </IfModule>
     
    <IfModule sapi_apache2.c>
        php_value register_globals 0
        php_value magic_quotes_gpc 0
        php_value session.auto_start 0
        php_value safe_mode 0
    </IfModule>
     
    <IfModule mod_rewrite.c>
     
        RewriteEngine On
            RewriteBase /cms/
     
    RewriteRule ^([^/]*)\.html$ content.php?pagename=$1 [L]
    RewriteRule content/([^/]*)/$ modules.php?module=$1 [L]
     
    </IfModule>
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml
    </IfModule>
     
  8. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    code mà em đưa là hoạt động trên .htaccess chứ không phải web.config. Em tìm lại code web.config trên mạng xem có không rồi chuyển qua sử dụng cái đó xem sao.

    hoặc .htaccess của em xóa đi những dòng sau

    Options -MultiViews -Indexes +FollowSymlinks
    ErrorDocument 404 /cms/404.php
     
  9. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    Chạy Magento trên plesk thì ta dùng web.config sau

    Mã:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Remove index.php Rule" stopProcessing="true">
                        <match url=".*" ignoreCase="false" />
                        <conditions>
                            <add input="{URL}" pattern="^/(media|skin|js)/" ignoreCase="false" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="index.php" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
     
  10. xenforo

    xenforo Thành Viên Mới

    Mình bị lỗi đường dẫn hình ảnh và một số link không hiểu sao nữa ?
    link diễn đàn mình có dạng http://domain/forum/
    Mong admin giúp với ! Thanks !
    Mình đang dùng Xenforo với style mặc định. Vào các bài viết thì được, nhưng chọn vào "Diễn đàn" --> http://domain/forum/ thì không thấy trang này. tắt đi thì oke.
     
  11. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    bạn vào thử /index.php có đc k? có đặt xenforo trong subfolder nào thì nhớ thêm vài rewritebase là folder đó nhé
     
  12. xenforo

    xenforo Thành Viên Mới

    Làm thế nào anh Thanh hướng dẫn giúp, em không rành IIS 7 ! Cảm ơn anh !
     
  13. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    bạn đang sử dụng .htaccess hay web.config? cho mình xem domain đc k :D
     
  14. xenforo

    xenforo Thành Viên Mới

    Em đang dùng WIndows 2008r2 + Plesk 11.0.9, trên máy chủ ở cơ quan (nội bộ)
    Cấu hình web.config như anh hướng dẫn
    Nó ra như thế này:
    + Vào forum con: http://serveribm/forums/forums/2/ ------chạy Ok
    + Vào bài viết: http://serveribm/forums/threads/1/ ------chạy Ok
    Nhấp vào chứ "Diễn đàn" http://serveribm/forums -------- Nó chạy ra trang http://serveribm/
    Gõ lại bằng tay http://serveribm/forums hay http://serveribm/forums/index.php cũng chạy ra trang http://serveribm/
    Nhưng gõ 2 link forum con và bài viết thì vẫn vào được
     
  15. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    em up file web.config của e lên,
     
  16. xenforo

    xenforo Thành Viên Mới

    Nhờ anh xem giúp ! Xin cảm ơn lắm lắm !
     

    Các file đính kèm:

    • web.zip
      Kích thước:
      525 bytes
      Đọc:
      645
  17. Nguyễn Thanh

    Nguyễn Thanh TND Staff Thành viên BQT

    Mình cũng gặp trường hợp này thường là dùng .htaccess rồi tìm cái dòng rewritebase / thay bằng rewritebase /forum là vào được.

    Nhưng cậu thử move web.config trong folder forum ra root xem có chạy đc k ? nếu k chạy đc thì chỉ còn cách move toàn bộ file trong forum ra root là chạy bình thường.
     
    xenforo thích bài này.

Chia sẻ trang này