simple mod_rewrite not working - php

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

Related

htaccess RewriteRule url frendly searches

I have a .htaccess file with this in it:
RewriteRule ^search/([a-zA-Z]+)$ index.php?page=search&search=$1
So basically it sends URLs like this:
url.net/search/this
To this:
url.net/?page=search&search=this
But when I send it a URL like:
url.net/search/this+search
I get an error returned as it doesn't know how to deal with +search bit.
Is there a way I can get it to include the + between words when the user clicks search?
I want it so that if the user types i+want+this+or+that or this+is+what+i+want+to+find, so no mater how long it is, it knows how to handle the parse to $_GET['search'] parameter.
You should be able to just include it in the regex...just remember to escape it,
RewriteRule ^search/([a-zA-Z\+]+)$ index.php?page=search&search=$1
Try this regex for the rewire rule:
RewriteRule ^search/([a-zA-Z].+)$ index.php?page=search&search=$1
Note the . before the + sign. Works as a regex here on this live PHP regex site. Yes, I know this is an Apache rewrite rule & PHP has no role at this stage, but basic regex logic should remain the same.

Issue with mod_rewrite query string rule

My issue is pretty simple however as I am new to mod_rewrite I seem to be very confused. basically I am trying to make vision friendly urls with mod_rewrite. ie: 'http://example.com/user.php?id=3' works the same as 'user/7854' etc. i currently have a regex rule as so:
RewriteEngine on
RewriteRule ^user/([a-z]+)/?$ user.php?id=$1
phpinfo() confirms that mod_rewrite is in fact operating/working. My understanding of this is that, if the user is directed to 'http://example.com/user/7854' (for example) The mod_rewrite regex will translate via apache to my php script: 'http://example.com/user.php?id=7854' because anything after 'user/' becomes captured. That being said, this is my user.php:
<?php
echo $_GET['id'];
?>
now as you can imagine 'http://example.com/user.php?id=7854' displays 7854 however 'http://example.com/user/7854' gives me an error (404) why is this? what have i misunderstood? I've spent an hour on numerous forums just to get this far, this concept isnt coming easy to me at all. cheers to any help.
Like #andrea.r says: you have rule for all characters but no numbers.
Change: RewriteRule ^user/([a-z]+)/$ user.php?id=$1
to: RewriteRule ^user/([a-z,0-9]+)/$ user.php?id=$1
User ID is numerical and you've written [a-z] instead of [0-9].
Try this: RewriteRule ^user/([0-9]+)/?$ /user.php?id=$1

Simple mod rewrite with PHP Get values

can someone tell me what in the world I'm doing wrong?
trying to take:
search.php?key=23&category=testing
and make:
/23/testing.html
with this:
RewriteRule ^([^/]*)/([^/]*)\.html$ /?key=$1&category=$2 [L]
For the life of me I can't figure this out...or if there is a better way, please point me in the right direction.
** UPDATE
Ok, after giving up on this I finally contacted the hosting company to see if there was a problem with my domain that was keeping this from working. Here is what they told me:
You do not have a rewrite condition and you only have a simple rewrite. For the code to >always work properly you should normally set a "RewriteCond" with a valid pattern.
You have used the "?" question mark character in your code although this is an escape >character and doing so would only cause a rewrite to index.php based upon your code as >everything after the question mark gets escaped.
Please consider using a "RewriteCond" in your code and also use regexes to properly escape >the "?" character and avoid unexpected behavior.
Can anyone give me some help? Thanks!
RewriteRule ^/([^/]*)/([^/]*)\.html$ /?key=$1&category=$2 [L,R]

htaccess regular expression with a "/"

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

Please Verify the htaccess code

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.

Categories