简体   繁体   English

找到并替换所有? 和= by / htaccess

[英]find and replace all the ? and =by / htaccess

hey all i am new to htaccess and i need to find and replace all the occurence of ? 嘿,我是htaccess的新手,我需要查找和替换所有出现的问题? and = by / from the url by htacess 和=由/从URL到htacess

in my htaccess file the code is 在我的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\?id=([^\s&]+) [NC]
  RewriteRule ^ /%1? [R=301,L]


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

    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?id=$1 [L,QSA]

there are some pages on the site like page2.php?id=34 currently it is showing like page2?id=34 but i want it like page2/id/34 and same for all the other pages 该网站上有一些页面,如page2.php?id = 34,目前它的显示像是page2?id = 34,但我希望它像page2 / id / 34一样,其他所有页面都一样

You have everything you need already in your htaccess almost. 您几乎已经在htaccess中拥有了所需的一切。 You direct everything except files and directories that do not exist to page.php 您将除文件和目录以外的所有内容都定向到page.php

Then in your PHP you rewrite urls like /page.php/id/2 to what you want. 然后在您的PHP中,将/page.php/id/2之类的URL重写为所需的URL。

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  RewriteRule . /page.php$1 [L,QSA]

You then just need to parse these url's in PHP inside page.php or index.php as many people use. 然后,您只需要在许多人使用的page.php或index.php内部的PHP中解析这些URL。

<?php
  $request  = str_replace("/page.php", "", $_SERVER['REQUEST_URI']);
  $params     = split("/", $request);
?>

Further reading on the idea of pretty urls here: http://forum.codecall.net/topic/74170-clean-urls-with-php/ and http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049 在此处进一步阅读漂亮网址的想法: http : //forum.codecall.net/topic/74170-clean-urls-with-php/http://code.tutsplus.com/tutorials/using-htaccess-files -用于-漂亮的URL -网6049

You can have: 你可以有:

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\?id=([^\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} /page2\.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 ^(page2)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]

# recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule ^(page2)(?:\.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?id=$1 [L,QSA]

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

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