htaccess RewriteRule not work - php

My first question here hope to find the right answer to my problem.
Here it is:
I try to transform http://mysite.it/home.php?pagina=ciao to http://mysite.it/ciao
I put inside my .htaccess this:
RewriteEngine On
RewriteRule ^home.php?(.*)$ $1
It shows me: http://mysite.it/?pagina=ciao
I know that something is wrong in the RewriteRule but I don't understand how to pick only after home.php?pagina= to take ciao,I have this on my PHP page $get= #$_GET ['pagina'];
I say thanks in advice and hope to resolve this.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /home\.php\?pagina=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /(home)\.php\s [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^(home)/?$ $1.php [L,NC]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ home.php?pagina=$1 [L,QSA]

I believe what you're looking for is :
RewriteEngine On
RewriteRule ^([A-Za-z]+)$ /home.php?pagina=$1
I hope you're not doing :
require_once($get);
after that code, since that would be very dangerous...

Related

htaccess code break down and some explanations

I've the following .htaccess file code
RewriteEngine On
RewriteBase /claimspro/
##-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php
##-----Prevent Directory File listing in all folder---
IndexIgnore *
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/ $1.php?caseid=$2 [L,QSA]
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]
The code above work the way I wanted it to be worked. It does change the url from this localhost/claimspro/afile.php?caseid=11 to localhost/claimspro/afile/caseid/11/
But when I add the another parameter i.e. /caseid/11/picid/12/ through the following way it behave weirdly.
Can I ask for some explanations of the code so can manage to get some result working in my favour.
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]
First if may I ask what is this at the end of the first line (above) &?([^\ ]*) how would I have to modify this to add another parameter such as RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)\&?([^\ ]*). What do I have to do here please if you could explain what I'm doing wrong here.
Secondly this line RewriteRule ^ %1/caseid/%2/?%3 [L,R] what is ?%3 [L,R] this %3 is representing what?
basically I want to add one more parameter in my htaccess file to cater the following
localhost/claimspro/anotherfile.php?caseid=11&picid=22
What additions should I make to my code?
Any idea?
Let me answer your 2 questions.
Answer to first question: the pattern &?([^\ ]*) is matching the rest of the query string (if present).
Answer to second question: ?%3 [L,R] is appending the captured data (from first question) as a query string (the R flag is used to redirect with a 302 by default).
If i had to do it, i would have written it that way
Options -Indexes
ErrorDocument 404 /claimspro/filenotfound.php
RewriteEngine On
RewriteBase /claimspro/
RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)&picid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/picid/%3/? [R=301,L]
RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/? [R=301,L]
RewriteCond %{THE_REQUEST} /([^/]+)\.php\s [NC]
RewriteRule . %1/? [R=301,L]
RewriteRule ^([^/]+)/caseid/([0-9]+)/picid/([0-9]+)/$ $1.php?caseid=$2&picid=$3 [L]
RewriteRule ^([^/]+)/caseid/([0-9]+)/$ $1.php?caseid=$2 [L]
RewriteRule ^([^/]+)/$ $1.php [L]
I can't explain the code because i'm not an expert in mode rewriting you can change your code withe the following code and it would work
RewriteEngine On
RewriteBase /claimspro/
#-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php
#-----Prevent Directory File listing in all folder---
IndexIgnore *
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/picid/%3/?%4 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/picid/([0-9]+)/ $1.php?caseid=$2&picid=$3 [L,QSA]
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]
Let me know if it works
Regards

replacing Login URL to short URL with .htaccess

I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]

rewrite url with slash using htaccess

Hello someone pls help me
I have this url which is dynamic
productSearch.php?id_category=8&id_size=23&id_colour=93
and i have to do this in
productSearch/8/23/93
i have try
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+id_category(?:\.php)?\?id=([0-9]+) [NC]
RewriteRule ^ id_category/%1? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
its remove .php only but not putting slash.
Pls some one give me some idea
Try this
RewriteEngine On
RewriteRule productSearch/(\d+)/(\d+)/(\d+)$ productSearch.php?id_category=$1&id_size=$2&id_colour=$3 [L]
RewriteEngine On
RewriteBase /
Options -MultiViews
RewriteRule ^productSearch/([^/]*)/([^/]*)/([^/]*)$ productSearch.php?id_category=$1&id_size=$2&id_colour=$3 [L]
Try this. Put in your url following links: lala
If you want that only a numeric path will be rewritten you could use ([0-9]+) instead of ([^/]*)

.htaccess Remove index.php remove query and leave string

I have been working on an .htaccess file. The url I started with was, www.example.com/index.php?page=pagetitle. I want the Link to only show www.example.com/pagetitle.
So far the only thing in my .htaccess file removes the index.php.
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.(php|html?)
RewriteRule ^ /%1 [R=301,L]
Any help would be greatly appreciated, I sadly don't have the knowledge to create these codes on my own.
Thank you in advance.
I will also be editing this question as I figure more of this out, from support of other people etc.
You can try these rules:
Options -MultiViews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Categories