简体   繁体   中英

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 ? and = by / from the url by htacess

in my htaccess file the code is

 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

You have everything you need already in your htaccess almost. You direct everything except files and directories that do not exist to page.php

Then in your PHP you rewrite urls like /page.php/id/2 to what you want.

  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.

<?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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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