使用的.htaccess动态URL重写重写、动态、htaccess、URL

2023-09-02 09:46:20 作者:모호한 丶 暧昧丶

我是pretty的新的.htaccess重写,我试图创建规则来动态地重写这些URL。

举例来说,假设用户输入以下网址:

  http://example.com/xxx/?user=2002
 

这将是改写成:

  http://example.com/xxx/user/2002/
 
iis重写不支持rewritebase

如果用户通过这样的多个参数:

  http://example.com/xxx/?user=2002&activity=100&result=true
 

这应该成为:

  http://example.com/xxx/user/2002/activity/100/result/
 

请注意:所有的查询字符串将动态生成。

这就是我想出了:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /新闻/

重写规则^指数 .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。 /news/index.php [L]
< / IfModule>
 

更新

我试图使上述code和查询字符串重写code一起工作。修改后的.htaccess看起来如下:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /新闻/
重写规则^指数 .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。 /news/index.php [L]

#先取出查询字符串从你/ XXX /网址
的RewriteCond%{QUERY_STRING} ^ + $
重写规则^新闻/?$%{REQUEST_URI} /%{QUERY_STRING}? [L]

#现在转换及放大器;至 /
重写规则^([^&功放;] +)及$ $ 1 / $ 2 [L](*)。

#现在转换=到/
重写规则^([^ =] +)=([^ =] + =。*)$ $ 1 / $ 2 [L]
重写规则^([^ =] +)=([^ =] +)$ $ 1 [L,R]

#内部规则替换/ N1 / V1 / N2 / V2到QUERY_STRING
重写规则^(新闻)/([^ /] +)/([^ /] *)(/.*)?$/ $ 1 $ 4〜$ 2 = $ 3 [L,QSA]
< / IfModule>
 

解决方案

真正棘手的规则,这些都是。把这些递归规则,在你的根的.htaccess:

  RewriteEngine叙述上
的RewriteBase /新闻/

#先取出查询字符串从您的网址
的RewriteCond%{THE_REQUEST} ? S +
重写规则^ /?$%{QUERY_STRING}? [L]

#现在把所有和放大器;至 /
重写规则^([^&功放;] +)及$ $ 1 / $ 2 [L](*)。

#现在将所有=到/
重写规则^([^ =] +)=([^ =] + =。*)$ $ 1 / $ 2 [L]
重写规则^([^ =] +)=([^ =] +)$ $ 1 / $ 2 [L,R]

#最后一个内部规则,以替换/ N1 / V1 / N2 / V2到QUERY_STRING
重写规则^([^ /] +)/([^ /] *)(?:/(*))?$?$ 3 $ 1 = $ 2 [L,QSA]

##你现有的东西,其次,现在
重写规则^指数 .PHP $  -  [L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
 

I am pretty new to .htaccess rewrites, and I'm trying to create rules to dynamically rewrite the URLs.

For example, say the user enters the following URL:

http://example.com/xxx/?user=2002

It would be rewritten into:

http://example.com/xxx/user/2002/

If user passes multiple parameters like this:

http://example.com/xxx/?user=2002&activity=100&result=true

It should become:

http://example.com/xxx/user/2002/activity/100/result/

Note: All the query strings will be dynamically generated.

This is what I have come up with:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/

RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>

Update

I tried to make the above code and query string rewrite code to work together. The modified .htaccess looks like below:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

# first take out query string from your /xxx/ URLs
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^news/?$ %{REQUEST_URI}/%{QUERY_STRING}? [L]

# now convert & to /
RewriteRule ^([^&]+)&(.*)$ $1/$2 [L]

# now convert = to /
RewriteRule ^([^=]+)=([^=]+=.*)$ $1/$2 [L]    
RewriteRule ^([^=]+)=([^=]+)$ $1 [L,R]

# internal rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule "^(news)/([^/]+)/([^/]*)(/.*)?$" /$1$4?$2=$3 [L,QSA]
</IfModule>

解决方案

Really tricky rules these are. Put these recursive rules in your root .htaccess:

RewriteEngine On
RewriteBase /news/

# first take out query string from your URLs
RewriteCond %{THE_REQUEST} ?S+
RewriteRule ^/?$ %{QUERY_STRING}? [L]

# now convert all & to /
RewriteRule ^([^&]+)&(.*)$ $1/$2 [L]

# now convert all = to /
RewriteRule ^([^=]+)=([^=]+=.*)$ $1/$2 [L]
RewriteRule ^([^=]+)=([^=]+)$ $1/$2 [L,R]

# finally an internal rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule "^([^/]+)/([^/]*)(?:/(.*))?$" $3?$1=$2 [L,QSA]

## Your existing stuff followed now
RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

 
精彩推荐
图片推荐