Url-rewriting with multiple parameters - php

DirectoryIndex /home.php
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/([0-9]+) product.php?id=$1&product_name=$2 [L,QSA]
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
I have this .htaccess file when i try this with 1 parameter it works but with 2 it doesnt work and i want to turn this product.php?id=1&p_name=exmpl link into product/1/exmpl with the above code i have to write this product/1/exmpl with my hand

Related

Nice url with htaccess with specific parameters

I want to have this form of URL : https://domain(:port)/lang/x/y/z/other
With this htaccess file, I can't achieve exactly what I want.
If I enter the URL https://localhost:port/lang/, I lose the port.
If I enter the URL https://localhost:port/lang, i got the URL https://localhost:port/langx/y/z
Please help me!
Options +FollowSymLinks
RewriteEngine On
#LIVE
RewriteBase /
#--------------------------------------
#-- 404 & 500 Redirects
#--------------------------------------
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
#--------------------------------------
#-- Force HTTPS
#--------------------------------------
RewriteCond %{HTTP_HOST} !=localhost$ [NC]
RewriteCond %{SERVER_PORT} !=8088
RewriteCond %{SERVER_PORT} !=8080
RewriteCond %{HTTPS} !=on
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(actions)($|/) - [L]
#--------------------------------------
#-- Add trailing PHP and Params
#--------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3&param3=$4&param4=$5 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3&param3=$4 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?param1=$2&param2=$3 [B,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#check for the existance of a PHP file first or else the 404 won't work
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php?param1=$2 [B,QSA,L]
RewriteRule ^(.*)/$ $1 [R=301,L]

ErrorDocument, remove .php and force https .htacces

I have
ErrorDocument 404 https://demo.com/404
#Remove .php
Options Indexes FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php
#Force https and without www
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
This isn't work ErrorDocument 404, I want you to do this:
www.demo.com -> https://demo.com
www.demo.com/page-not-exits -> https://demo.com/404
www.demo.com/page.php -> https://demo.com/page
http://demo.com -> https://demo.com
Greetings and thank you!
Solution
Finally this works:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
#errorredirect
ErrorDocument 404 http://demo.com/404.php
#remove .php
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>
thanks #rushil-pachchigar
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#errorredirect
ErrorDocument 404 http://example.com/404.php
#remove .php
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>
You can use the following htaccess
ErrorDocument 404 https://demo.com/404
RewriteEngine on
#force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://demo.com/$1 [L,NE,R]
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
Try below rules,
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ 404 [R=301,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([\w-]+) $1.php [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]

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

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]

Categories