2016-09-13 35 views
2

我需要重写部分我的url作为参数和部分路径。是否有可能,如果这样任何人都可以请帮我htaccess重写URL的一部分,并保持原样

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

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] 

,但由于一些参数的变化,我不知道该怎么办

这是我整个的htaccess到目前为止

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

方法#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] 
+0

虽然你的第一个方法看起来很有前途,但它给了我404错误 – krzysztof

+0

@krzysztof确保你有'选项-MultiViews'启用 –

+0

是我有正确的选项启用,我甚至可以说你的第二个方法是工作 - 它需要一个问题处理htaccess中的每个场景,我在寻找更普遍的东西,因为我在php中更好。 – krzysztof

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 ## 
+0

它给我内部服务器错误 – krzysztof

+0

@krzysztof对不起,我错过了领先的'/'那里。现在检查 – hjpotter92

+0

仍然是一样的结果 – krzysztof