htaccess code break down and some explanations - php

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

Related

RewriteRule blocking RewriteCond & RewriteRule after it

If this is a duplicate then I am really sorry, but I used google and also search here and could not find anything that would work for me. Maybe I just don't know what exactly to search for.
Anyway, back to my question:
I am trying to change old ugly URLs to nicer urls (wanted to have news/XXX/XY but relative links defeated me), so I wrote couple lines of code to my .htaccess and tested them (well, I actually used same code on my other page for redirect, so I know it does work). I get redirected from /news.php to /news, but the query string magic is not working at all...
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
None of the RewriteRules after first one gets executed. But if I change my .htaccess like this:
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news?$ /news.php [L,NC]
then it works. I tried using /\?bla - /news\?bla - /news\.php\?bla etc. Nothing actually worked.
Does somebody have any Idea why its behaving like this and how to fix it? It looks like it does not recognize the news after it starts loading from news.php file.
I am completly lost, as I don't work with .htaccess that often. Any ideas?
I actually ended up using this:
#########################
# NICE URLS QUERIES #
#########################
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-page-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\(?:\.php|)\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news-([0-9]+)-([0-9]+)?$ news.php?readmore=$1&rowstart=$2 [L,QSA,NC]
RewriteRule ^news-([0-9]+)/?$ news.php?readmore=$1 [L,QSA,NC]
RewriteRule ^news-page-([0-9]+)/?$ news.php?rowstart=$1 [L,QSA,NC]
#######################
# OTHER NICE URLS #
#######################
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
The first thing you need to make sure you're doing is putting all the redirects first. That is, every rule that has the R flag needs to be before the rules that don't. But it looks like you're not matching the - style URLs to rewrite them BACK to the version with the query string. So I think you really want something like this (you can get rid of the .php extension while you get rid of the query string):
RewriteCond %{THE_REQUEST} \ /+news\.php(\ |$)
RewriteRule ^ /news [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?rowstart=([\ &]+)
RewriteRule ^ /news-row-%1? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([^&]+)&rowstart=([\ &]+)
RewriteRule ^ /news-%1-%2? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([\ &]+)
RewriteRule ^ /news-%1? [L,R=301]
## Now, internally rewrite the request back to the query string and php extension
RewriteRule ^news$ /news.php [L]
RewriteRule ^news-row-(.*)$ /news.php?rowstart=$1 [L,QSA]
RewriteRule ^news-(.*)-(.*)$ /news.php?readmore=$1&rowstart=$2 [L,QSA]
RewriteRule ^news-(.*)$ /news.php?readmore=$1 [L,QSA]

htaccess- unable to remove file extension from url php

I am trying the following rules to remove file extension from file name but the problem is it is showing that it is removed but when I login again it is not redirecting to my home page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase \
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank You
In your redirection just type in page name with no extension, eg. header('Location: index'); or redirect_user('index'); and use the code below in your .htaccess file. now the link on the browser will be say "localhost/index"
RewriteEngine on
RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1 [QSA,L]
I think your rules are just a little bit off. You are using THE_REQUEST to redirect php extension to non php extension but you are not using a / after %1 in the rewriterule so when the next rule is read it is not matching because it will only internally redirect if there is a / in that rule. So either add a forward slash after %1 in the second rule or make the backslash optional in the last rule. I would just make it optional in last rule so it will match either way.
Try this update and see how it works.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /([^&\ ]+).php [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [NC,L]

htaccess RewriteRule not work

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...

.htaccess Remove .php keep regular indexing

I'm using:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
This makes it so I don't have to use ".php" at the end of my pages. However, if I don't specify an index it gives a 404.
Example:
example.com/content.php is directed to example.com/content
^ That works perfectly well.
However, if I want to go to a directory like example.com/users it gives a 404 error.
Only example.com/users/index works.
I want to be able to remove .php the way it is doing already, but I still want directories to work without specifying a page(automatically display index.php). I'm not very fluent in regex or htaccess coding so I have been unable to tweek this myself.
Any help would be appreciated. Thank you!
Replace your code with these 2 rules:
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Simple htaccess rewrite/redirect to search page

I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]

Categories