find and replace all the ? and =by / htaccess - php

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]

Related

how to replace ?= and & from url with / by htaccess in PHP CI

Hello all i am new with htaccess and i dont know how to make my url SEO friendly, please do guide me little here
here is my url domain.com/article?seo=What-Software&id=7
required url domain.com/article/What-Software/7
can also accept as domain.com/article/What-Software/id/7
here is my current htaccess
RewriteEngine On
# remove /index.php/main/ from URLs
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+index\.php/main/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^index\.php/main(/.*)?$ https://%1$1 [L,NC,NE,R=301]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Header set Access-Control-Allow-Origin "*"
</IfModule>
# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# remove main
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /main [NC]
RewriteCond %{REQUEST_URI} ^(.*/)main$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Header set Access-Control-Allow-Origin "*"
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
please guide me how to do that as i need to make the url simpler for SEO
also i dont want to messup with any form post

how to redirect old url with parameters to new seo url using htaccess

want to redirect param urls to seo friendly urls
https://example.com/a.php?slug=vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present
to
https://example.com/vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present
and
https://example.com/tag.php?id=Virtual-reality
to
https://example.com/tag/Virtual-reality
my current .htaccess code
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ a.php?slug=$1 [L]
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# external redirect to redirect old URL to pretty URL
RewriteCond %{THE_REQUEST} /a\.php\?slug=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{THE_REQUEST} /tag\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /tag/%1? [R=301,L,NE]
RewriteRule ^tag/([\w-]+)/?$ tag.php?id=$1 [L,QSA,NC]
# internal rewrite to forward to actual URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ a.php?slug=$1 [L,QSA]

PHP htaccess multiple variables

Hi I'm using the following htaccess.
RewriteEngine On
RewriteBase /JustSnacksNew/
RewriteCond %{THE_REQUEST} /Products\.php\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([A-Z,0-9,a-z-]+)$ Products.php?category=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /ViewProduct\.php\?category=([^\s&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /ViewProduct\?category=([^\s&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([A-Z,0-9,a-z-]+)$ ViewProduct.php?category=([^\s&]+)&product=$1 [L,NC,QSA]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
In the first rule i'm passing one variable.It's working fine.But in the second rule i'm passing two variables.But it is redirecting to the first rule.
Please help me to fix this.
First url i'm passing is : ./Products.php?category=catname
Second url passing is : ./ViewProduct.php?category=catname&product=product
You can use:
RewriteEngine On
RewriteBase /JustSnacksNew/
RewriteCond %{THE_REQUEST} /ViewProduct(?:\.php)?\?category=([^&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([A-Z,0-9-]+)/?$ Products.php?category=$1 [L,NC,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewProduct.php?category=$1&product=$2 [L,NC,QSA]

add new rule to my htaccess file to replace ? by /

here is my htaccess file please take a look
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
i am new to htaccess please help me
and one important thing is that i should able to fetch the get variable fro id in my php code
$idis=$_GET['id'];
Your rules are getting pretty complicated. With that regex pattern and ordering has become much more important. Have your full .htaccess as this:
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]
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]

replacing ? by / and triming some text from url by htaccess

Hello all i am having a website where i have htaccess file the code inside the file seems like
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|jpeg)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(page1)\.php\?eid=(78)[&\s] [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteRule ^(page1)/(\d+)$ /$1.php?eid=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+page2\.php\?url=([^\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 ^(.+)$ /page2.php?url=$1 [L,QSA]
The file successfully redirects the page2.php?url=xyz to page2/xyz but it directs the page1.php?eid=3 to page1?eid=3 besides this i also needs to direct the page3.php?did=3 to page3/3
I need the page2.php?eid=3 to be directed to page2/3
and page3.php?did=4 to page3/4
page1 is directed fine.
Can any one help me actually i am not so good in htaccess but i have tried to manipulate the htacces file but no help.
Thanks

Categories