I just know how htaccess works but I am always confused with the writing syntax and I appreciate if anyone could help me solving the below htaccess issue.
I have couple pages linking to redirect to something like
http://mydomain.com.au/product-details.php/142/categoryAbstract
but due to the mistakes of previous developer the images are not loading unless that url is
http://mydomain.com.au/product-details.html/142/categoryAbstract
He converted all php pages to html (I really don't know what's this intention in doing that) but
now the url should work even if it as http://mydomain.com.au/product-details.php/142/categoryAbstract
He used the below htaccess for this but its not working. If I manually change the url from .php to .html everything working fine.
RewriteRule ^product-details.html/(.*)/(.*)$ product-details.php?productid=$1&category=$2
I need a working line of code so that even the url http://mydomain.com.au/product-details.php/142/categoryAbstract should work.
You will just need an OR group (a|b) to account for both possibilities:
RewriteRule ^product-details\.(html|php)/(.*)/(.*)$ product-details.php?productid=$1&category=$2
#---------------------------^^^^^^^^^^^
That can be improved a little though. The (.*) are greedy matches. You are better served to use ([^/]+) as the first grouping to match everything up to the next /. I have also escaped the dot as \. so it is matched as a literal instead of any character.
RewriteRule ^product-details\.(html|php)/([^/]+)/(.*)$ product-details.php?productid=$1&category=$2
The .php extension is commonly modified either through rewriting or actual file renaming and server configuration to parse .html as .php in order to hide some server-side information from end users. To prevent them from knowing what technologies the site runs on the back end. It less common to actually rename files to .html than to use URL rewriting to hide the .php, however.
RewriteRule ^product-details.html/(.*)/(.*)$ product-details.php?productid=$1&category=$2
What this rule does is take everything after product-details.html/ and before the last / and a second bit gets taken after the last / until the end of the line. then it takes those bits and puts them where the $1 and $2 are.
to change it so it accepts .html and .php you can change it with
RewriteRule ^product-details(.html|.php)/(.*)/(.*)$ product-details.php?productid=$2&category=$3
Because it looks like the first bit you are grabbing are numbers and (.*) is a greedy selector it may be better to replace it with ([0-9]*) which will only select numbers. that way if you ever have /s in your catagory you'll be fine. giving you:
RewriteRule ^product-details(.html|.php)/([0-9]*)/(.*)$ product-details.php?productid=$2&category=$3
Related
I am trying to trap old URL's of the form:
http://www.example.com/mpn_engine.php%3Ffamilyname%3Djiyalal+goswami%26menuopt%3D2%26submenuopt%3D1%26Search%3Dstuff
In my .htaccess file, with the help of various wise StackOverflowers as RegEx is alien to me, I have arranged to catch the PHP script 'mpn_engine.php' (both .php3 and newer .php copies) wherever it might be found (in any sub folder) and redirect visitors to the index page.
RewriteRule (^|/)mpn_engine\.php$ /index.html? [L,NC,R=301]
RewriteRule (^|/)mpn_engine\.php3$ /index.html? [L,NC,R=301]
The odd thing I am finding is that the above seems to work providing I seek after the php files exactly, or if I supply conventional parameters of the form:
http://www.example.com/lang/mpn_engine.php?x=fred
but as soon as I substitute a percent mark for the question mark, i.e. something like the following:
http://www.example.com/lang/mpn_engine.php%x=fred
The Rewrite fails, & and I get unpredictable results, usualy a a 404 but occassionally a 'Bad Gateway'.
How can I rewrite this ReWriteRule to catch this .php file in any folder it might be looked for and with any trailing characters, including a percent sign, and redirect it gracefully to the index page?
Thanks!
Your question has a number of sub-questions:
If you want to "catch this .php file in any folder it might be
looked for" then as long as your .htaccess file is in the root folder of your website (and not in a subfolder), then you are covered.
If you want to cover ANY trailing character, then you can make one of two changes to your rewrite rule:
Remove the ending $:
RewriteRule (^|/)mpn_engine\.php /index.html? [L,NC,R=301]
or
Add a wildcard after "php":
RewriteRule (^|/)mpn_engine\.php(.*)$ /index.html? [L,NC,R=301]
In the first case, if the $ present, this tells Apache to ONLY match if "php" is at the end of the URL. In the second case, this tells Apache to match if "php" is followed by zero or more of any other characters at the end of the URL. In either case, you do not need your second rewrite rule concerning "php3" -- either of these above will match for those instances as well.
The reason your first example with the "%" worked but subsequent attempts gave 404 errors is because the server translates "%3F" to "?", and "?" has a special meaning for web servers and is essentially ignored by your regex matcher -- thus the server acts as if "php" is the final part of the URL, and the rewrite succeeds.
Let's say I have a web-page called www.mysite.com
How can I make it so whenever a page is loaded like www.mysite.com/58640 (or any random number) it redirects to www.mysite.com/myPHPpage.php?id=58640.
I'm very new to website development so I don't even really know if I asked this question right or what languages to tag in it...
If it helps I use a UNIX server for my web hosting with NetWorkSolutions
Add this to your .htaccess file in the main directory of your website.
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9]+)$ myPHPpage.php?id=$1 [L]
Brief explanation: it says to match:
^ from start of query/page
[0-9] match numbers
+ any matches of 1 or more
$ end of page requested
The parentheses part say to look for that bit and store it. I can then refer to these replacement variables in the new url. If I had more than one parentheses group then I would use $2, $3 and so on.
If you experience issues with the .htaccess file please refer to this as permissions can cause problems.
If you needed to capture something else such as alphanumeric characters you'd probably want to explore regex a bit. You can do things such as:
RewriteRule ^(.+)$ myPHPpage.php?id=$1 [NC, L]
which match anything or get more specific with things like [a-zA-Z0-9], etc..
Edit: and #Jonathon has a point. In your php file wherever you handle the $_GET['id'] be sure to sanitize it if used in anything resembling an sql query or mail. Since you are using only numbers that makes it easy:
$id = (int)$_GET['id']; // cast as integer - any weird strings will give 0
Keep in mind that if you are not going to just use numbers then you will have to look for some sanitizing function (which abound on google - search for 'php sanitize') to ensure you don't fall to an sql injection attack.
I'm having a brain cramp. I'm using htaccess to rewrite a page and sometimes the variable that gets passed through will have a / (forward slash) in the variable. Sometimes there will be a slash and sometimes there won't but it is super important that all of this is treated as one variable. I'd really rather not reprogram all my pages with a str_replace() to switch a - for a / and then make a call to a database. For example:
http://www.example.com/accounting/finance.htm
Accounting/Finance is one variable that I need.....it is not in an accounting directory and then there's a page called finance.htm in accounting. So far I've got something like
RewriteRule ^([A-Za-z]+.*[A-Za-z]*)\.htm$ mypage.php?page=$1 [L,NC]
But it doesn't like it.
Can someone help me out?
Thanks in advance.
REPLY TO COMMENTS/ANSWERS
The specific rule that I'm looking for is something like this.....
[start of string]...1 or more letters...[possibility of a / followed by 1 or more letters].htm[end of string]
The two answers given below aren't working...I'm pretty sure it keeps treating it as a directory and not an actual "filename". As soon as I remove the forward slash the page works just fine...
If i get you right, you just need this one:
([A-Za-z/]*)\.htm
it should work with every combination of / or not-/
e.g.
accounting/finance.htm
test.htm
A slash is just another character. Apart from that, your regexp looks unnecessarily complex. For instance, .*[A-Za-z]* is not different from .* and also [A-Za-z] can be shortened to [a-z] if you use the [NC] flag.
Your precise rules are not entirely clear, but you probably want something on this line:
RewriteRule ^([a-z/]+)\.htm mypage.php?page=$1
I was wondering how can I create clean urls using PHP. Do I do this all in PHP or do I need to use mod_rewrite in some way? Can someone explain this to me in laymans terms?
Here is my current url a element link and how it looks in the browser
http://www.example.com/members/1/posts/page.php?aid=123
But I want it to read the pages title.
http://www.example.com/members/1/posts/title-of-current-page/
First you need to generate "title-of-current-page" from PHP, using this function eg:
function google($string){
$string = strtolower($string);
$string = preg_replace('/[^a-zA-Z0-9]/i','-',$string);
$string = preg_replace("/(-){2,}/",'$1',$string);
return $string;
}
Second thing, you need to make a rewrite, but you should keep aid in form of "/123-title-of-current-page"
Rewrite would go something like this (I am ignoring your entire URL)
RewriteRule ^([0-9]+)-(.*?)$ page.php?aid=$1 [L,QSA]
You can do this using mod_rewrite:
You'll need to edit a file called .htaccess at the top level of your web folder. This is where you can specify certain settings to control the way Apache accesses items in this folder and below.
First things first. Let's turn on mod_rewrite: RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
The rule matches any URL which is formed of lower case letters, followed by a /, then more lower case letters and/or hyphens, and appends .php to the end. It keeps track of anything wrapped in brackets () and refers to them later as $1 and $2, i.e. the first and second match. So if someone visits these URLs:
http://example.com/weblog/archive
it will be converted to following:
http://example.com/weblog/archive.php
You will find more details on :
http://wettone.com/code/clean-urls
You have to use a rewrite to direct all requests to an existing php file, otherwise you get all 404 not found errors because you are trying to get a page that simply is not there.
Unless you rewrite your 404 page to handle all requests and you definitely donĀ“t want to go there...
Is it possible to use .htaccess to process all six digit URLs by sending them to a script, but handle every other invalid URL as an error 404?
For example:
http://mywebsite.com/132483
would be sent to:
http://mywebsite.com/scriptname.php?no=132483
but
http://mywebsite.com/132483a or
http://mywebsite.com/asdf
would be handled as a 404 error.
I presently have this working via a custom PHP 404 script but it's kind of kludgy. Seems to me that .htaccess might be a more elegant solution, but I haven't been able to figure out if it's even possible.
In your htaccess file, put the following
RewriteEngine On
RewriteRule ^([0-9]{6})$ /scriptname.php?no=$1 [L]
The first line turns the mod_rewrite engine on. The () brackets put the contents into $1 - successive () would populate $2, $3... and so on. The [0-9]{6} says look for a string precisely 6 characters long containing only characters 0-9.
The [L] at the end makes this the last rule - if it applies, rule processing will stop.
Oh, the ^ and $ mark the start and end of the incoming uri.
Hope that helps!
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([0-9]{6})$ scriptname.php?no=$1 [L]
</IfModule>
To preserve the clean URL
http://mywebsite.com/132483
while serving scriptname.php use only [L].
Using [R=301] will redirect you to your scriptname.php?no=xxx
You may find this useful http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/pdf/
Yes it's possible with mod_rewrite. There are tons of good mod_rewrite tutorials online a quick Google search should turn up your answer in no time.
Basically what you're going to want to do is ensure that the regular expression you use is just looking for digits and no other characters and to ensure the length is 6. Then you'll redirect to scriptname.?no= with the number you captured.
Hope this helps!