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

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

Related

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]

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]

htaccess redirection working for on file but not working for other files

Below is my htaccess file.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
ErrorDocument 404 /404.php
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?id1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ profile_view.php?id1=$2&name2=$3 [NC,L]
and here are my urls/
http://localhost/profile_view/1/test
http://localhost/view_campaing/1/test-campaign-name
with some php code my link for view_campaign is redirecting properly. i can say my links for view_campaign is working fine. if it appears like below.
http://localhost/view_campaign/1/test-campaign-name-some-junk
http://localhost/view_campaign/1
http://localhost/view_campaign/1/
I want the same for profile_view. i copy pasted the same code in htaccess/php file and change relevant things.
Issue is if i try to open http://localhost/profile_view/1/test or http://localhost/profile_view/1 or http://localhost/profile_view/1/
it is redirecting to
http://localhost/view_campaign/1/test-campaign-name-some-junk
I don't have much understanding about htaccess.
Please advise.
Have it this way:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(view_campaign|profile_view)/(\d+)$ $1.php?var1=$2 [L,NC,QSA]
RewriteRule ^(view_campaign|profile_view)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L,QSA]
I make some changes in anubhava answer. Now, it is working fine.
Below is the working code.
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^(view_campaign)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]
RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?var1=$1 [L]
RewriteRule ^(profile_view)/(\d+)/([^/]+)$ profile_view.php?var1=$2&var2=$3 [NC,L]

htaccess rewrite url and redirect

I am wondering how to rewrite this url profile.php?user=1 to /user/1.
I know that this can be done by .htaccess. Here is what i have tried and it worked fine but the only problem is that it
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]
Besides these codes, my .htacces also has these lines
RewriteEngine On
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]
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteCond %{THE_REQUEST} \s/+page\.php\?pagename=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?pagename=$1 [L,QSA]
Please also tell me if my .htaccess page is correct. I am not good at htaccess
Change this:
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]
to:
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /user/%1? [R=302,L]
RewriteRule ^user/([^/]+)/?$ profile.php?user=$1 [L,QSA]
And make sure the page has either absolute links or add this to the page's header:
<base href="/" />
otherwise, all your links will be broken.
In htacess rewrite you want to in fact redirect a url structure to a different url
If you have a link to profile.php?user=1 you will have to change this like to this : profile.php/user/1 then tell your htacess to redirect this adresse to this profile.php?user=1
You can do this :
RewriteRule ^user/(.*)$ index.php?&user=$1
If you want to test out some htaccess I will recomande this usedul site : http://htaccess.madewithlove.be/

URL rewrite with two parameters

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]

Categories