URL rewrite with two parameters - php

I am stuck changing the htaccess file,
currently i am displaying pages like this after rewriting from htaccess:
www.domain.com/contact
which comes to be this url before rewriting from htaccess:
www.domain.com/index.php?show=contact
I am using this code to do the rewriting:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?show=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?show=$1 [L,QSA]
what i want to do now its set two parameters, eg:
www.domain.com/index.php?show=contact&id=3
which should display:
www.domain.com/contact
and inside that page i wanna be able to get ($_GET['id']) the id.
Please help me i am stuck...

Have a separate rule to handle /contact like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?show=([^\s&]+)&id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?show=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=302,L]
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?show=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?show=$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]

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]

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