mod_rewrite将URL路径转换为查询字符串

时间:2013-06-09 06:34:38

标签: .htaccess mod-rewrite

我想使用mod-rewrite转换:

https://newbootstrap.com/quiz 

到:

https://newbootstrap.com/index.php?url=quiz 

这是我的.htaccess:

    Options +FollowSymlinks
    Options -Indexes

    AddType text/x-component .htc

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml

    <IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ 
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text\.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image\.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* 
    </IfModule>

    <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
    Header set Cache-Control "max-age=2592000"
    </FilesMatch>

    #Запрет перехода с определенного сайта
    #<IfModule mod_rewrite.c>
    #RewriteEngine on
    #RewriteCond %{HTTP_REFERER} bannedurl1.com [NC,OR]
    #RewriteCond %{HTTP_REFERER} bannedurl2.com [NC,OR]
    #RewriteRule .* - [F]
    #</ifModule>

2 个答案:

答案 0 :(得分:1)

RewriteRule ^quiz$ /index.php?url=quiz [L]

或者,如果这是任何请求的通用规则:

RewriteRule ^(.+)$ /index.php?url=$1 [L]

答案 1 :(得分:1)

完成此任务的完整.htaccess文件为:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ /index.php?url=$1 [L]
</IfModule>