I would like some help regarding regex in .htaccess for url. I have this url I want to represent, I am pretty sure my regex code is accurate but when I load the page it gives me a 404 error. I am not sure but maybe I am missing something. Please help me out if you can.
original:
display-item.php?ic=70efdf2ec9b086079795c442636b55fb-17&n=Cosmetic%20For%20Women
ui has an md5 followed by a - then an id then n accepts a string with spaces
regex:
RewriteRule ^display/(/^[a-z0-9]{32}+\-[1-9][0-9]{0,10}$/)/(^[a-zA-Z0-9\.\s\-]+) view/display-item.php?ic=$1&n=$2
Related
I have a PHP script located at http://localhost/dir1/dir2/shift.php that I am passing urls as parameters to like so: http://localhost/dir1/dir2/https://google.com but I am getting a Forbidden you don't have access error. When I remove the : it works fine though. I have checked my regex with some online regex validation tools and it says that it should be working but it doesn't
RewriteEngine on
RewriteRule ^dir1\/dir2\/([A-Za-z0-9\!\#\#\$\%\^\&\*\(\)\_\-\+\=\{\}\[\]\;\:\'\"\<\,\>\.\?\|\~\`\s\/\\]+)\/? dir1\/dir2\/shift.php?url=$1 [L]
Any help would be great. Thanks!
You should not use reserved characters in URL.
Though you can URL encode and then pass it as a parameter. For example:
http://localhost/dir1/dir2/shift.php?
param=http%3A%2F%2Flocalhost%2Fdir1%2Fdir2%2Fhttps%3A%2F%2Fgoogle.com
And then in shift.php you can first URL decode the parameter and then use.
Im kinda new using htaccess and mod rewrite
I got every link to work as i want, but i have a problem when im trying to change the link for the forum everything goes kinda to hell..
This is my code
RewriteRule ^thread/([0-9]+)/([A-Za-z0-9]+)$ sidor/forum/showthread.php?threadID=$1&name=$2 [L]
When i try to get thread/1/forum it works perfectly but when i try to get a longer name like this
thread/2/cs-wont-work-for-me
The htaccess gets me a error 404..
And my links i change so (spaces) gets - and åäö gets aao
Anyone knows what the problem is?
Do you need more code? just post a comment and tell me that then i can try to give you a little more.
thread/2/cs-wont-work-for-me is not matched because you only check for alpha numeric characters ([A-Za-z0-9]+). Include dashes and any other characters you want to match to your regex. This should do what you want:
^thread/([0-9]+)/([A-Za-z0-9\-ÅÄÖåäö]+)$
As a sidenote, I can really recommend htaccess tester to debug issues like this.
You can use as
RewriteRule ^thread/([0-9]+)/(.*) sidor/forum/showthread.php?threadID=$1&name=$2 [L]
Does not check the last sequence of string of specific pattern
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 have a URL like this
http://www.freshupnow.com/movie.php?moviename=A+Flat
I want to rewrite this URL using .htaccess code like this
http://www.freshupnow.com/movies/A+Flat
So, I have used the following code for this
RewriteRule ^movies/([A-Za-z0-9-]+)/?$ movie.php?moviename=$1 [NC,L]
Can Some one please verify it, Either it is Right or Wrong?
([A-Za-z0-9-]+) should be ([A-Za-z0-9+])+ coz you want to capture the whole group 1 or more times and you also want to allow "+" character in your movie names, as in the example "A+Flat". You can validate your regex yourself using this online tool: Regex Validate. There are several others, try Googling for it.