简体   繁体   English

向我的 htaccess 文件添加新规则以替换 ? 经过 /

[英]add new rule to my htaccess file to replace ? by /

here is my htaccess file please take a look这是我的 htaccess 文件,请看一下

 ErrorDocument 404 /error404.php
 ErrorDocument 403 /error404.php
 Options -Indexes 
  RewriteEngine On
   RewriteBase /

   RewriteCond %{REQUEST_METHOD} POST [NC]
   RewriteRule ^ - [L]

   RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
   RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
   RewriteCond %{HTTP_HOST} !^www\.
   RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

   RewriteCond %{THE_REQUEST} \s/+page\.php\?brand=([^\s&]+) [NC]
   RewriteRule ^ /%1? [R=301,L]

  RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
   RewriteRule ^ /%1 [R=302,L,NE]

  ### rules to convert ?n1=v1&n2=v2 /n1/v1/n2/v2 

  RewriteCond %{THE_REQUEST} /profile\.php\? [NC]
  RewriteCond %{QUERY_STRING} ^([^&]+)&(.*)$
  RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]

   RewriteCond %{ENV:REDIRECT_QS} =1
   RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$
   RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]

   RewriteCond %{ENV:REDIRECT_QS} =1
    RewriteCond %{QUERY_STRING} ^[^&=]+$
   RewriteRule ^(profile)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]

    # recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
     RewriteRule ^(profile)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$ /$1.php$4?$2=$3 [L,QSA]

      ### end of convert ?n1=v1&n2=v2 /n1/v1/n2/v2

      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
       RewriteRule ^(.+?)/?$ /$1.php [L]

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d  
     RewriteRule ^(.+)$ /page.php?brand=$1 [L,QSA]

I want to add a new rule to this file which can replace this url www.domain.com/profile?id=2 to www.domain.com/profile/2我想向这个文件添加一个新规则,它可以将这个 url www.domain.com/profile?id=2替换为www.domain.com/profile/2

i am new to htaccess please help me我是 htaccess 的新手,请帮助我

and one important thing is that i should able to fetch the get variable fro id in my php code一件重要的事情是我应该能够在我的 php 代码中从 id 中获取 get 变量

 $idis=$_GET['id'];

Try:尝试:

RewriteCond %{THE_REQUEST} /profile\?id=([^\s]+) [NC]
RewriteRule ^ /profile/id/%1? [NC,L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/id/([^/]+)/?$ /profile/$1 [NC,L]

Your rules are getting pretty complicated.你的规则变得非常复杂。 With that regex pattern and ordering has become much more important.使用正则表达式模式和排序变得更加重要。 Have your full .htaccess as this:拥有完整的 .htaccess,如下所示:

ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]

RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{THE_REQUEST} \s/+page\.php\?brand=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [L,R=302]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]

### rules to convert ?n1=v1&n2=v2 /n1/v1/n2/v2 

RewriteCond %{THE_REQUEST} /profile\.php\? [NC]
RewriteCond %{QUERY_STRING} ^([^&]+)&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]

RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]

RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^[^&=]+$
RewriteRule ^(profile)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]

# recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule ^(profile)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$ /$1.php$4?$2=$3 [L,QSA]

### end of convert ?n1=v1&n2=v2 /n1/v1/n2/v2

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d  
RewriteRule ^ - [L]

RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [QSA,NC,L]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^(.+)$ page.php?brand=$1 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM