htaccess重写url的一部分并保持原样

时间:2016-09-13 17:22:23

标签: apache .htaccess mod-rewrite url-rewriting

我需要将部分网址重写为参数,并将部分重写为路径。是否有可能,如果有的话,请有人帮助我

更具体地说,我有一个这样的网址:

www.mysite/products/producer:some+producer/category:some+category/color:red,blue,yellow

我需要的是

www.mysite/products.php/producer:some+producer/category:some+category/color:red,blue,yellow

根据www.mysite/products/之后的用户查询路径,可以缩短或缩短

,使事情变得更有趣

如果我有一定数量的参数,我会写那样的

RewriteRule ^products/([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)/?$ products.php/$1/$2 [L]

但是随着一些参数的变化,我不知道该怎么做

到目前为止,这是我的全部内容

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^mysite\.pl
RewriteRule ^(.*)$ http://www.mysite.pl/$1 [R=permanent,L]
RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
AddDefaultCharset UTF-8
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
Header append Cache-Control "public"
</IfModule>
## EXPIRES CACHING ##

2 个答案:

答案 0 :(得分:2)

方法#1只是更改产品并保持其他所有内容

RewriteRule ^products/$ products.php [NC,L,B,QSA]

方法#2

RewriteRule ^products/producer:(.*)/category:(.*)/color:(.*)$ products.php?producer=$1&category=$2&color=$3 [NC,L]

这可能是你的htaccess文件应该是什么样子。

Options -MultiViews
RewriteEngine On

# Prevents directory browsing
Options -Indexes

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP

# your 404 page
ErrorDocument 404 /path/to/your/404file.php

RewriteRule ^products/$ products.php [NC,L,B,QSA]

答案 1 :(得分:1)

您可以使用.+支票:

RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]

将浮动指令放在各自的模块中检查:

Options -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^mysite\.pl
    RewriteRule ^(.*)$ http://www.mysite.pl/$1 [R=permanent,L]
    RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]
</IfModule>
AddDefaultCharset UTF-8

<IfModule mod_filters.c>
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
    </IfModule>
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 1 month"
    Header append Cache-Control "public"
</IfModule>
## EXPIRES CACHING ##