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]
Related
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
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
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'm struggling with a redirect problem. I need to redirect the following URL with mod_rewrite thorough .htaccess
http://www.mysite.com/somescript.php?&lang=php&var=1&var=2
to the following
http://www.mysite.com/somescript.php?lang=php&var=1&var=2
So, basically I just need to remove the
&
before
lang=php
However, the order is important. Sometimes
&lang=php
appears after other variables in the querystring. in this scenario I need the
&
to remain part of
&lang=php
Is this possible?
To summarise, if &lang=php appears at the beginning of the query string, remove the &. If &lang=php appears anywhere else in the query string, the & must remain.
Hope this is clear!
I would change the script myself but unfortunately I am not the developer, and he doesn't seem too helpful at the moment; this is a quick fix.
I would replace ?& with ?:
RewriteCond %{QUERY_STRING} ^\&(.*)$
RewriteRule ^somescript\.php$ /somescript.php?%1 [L,R=301]
why don't you match "?&" and replace it by "?" ?
Something like:
RewriteRule ^(.*)?&(.*) $1?$2 [L]
(not tested)
Because I think the combination "?&" is never valid...(?)
I'm trying to redirect an easy to remember url to a php file but I'm having some trouble with the regex.
Here's what I have at the moment:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}) /tcb/lerbd.php?autocarro=$1&tipo=$2&dsd=$3
It is working but only if I supply all 3 arguments. I want the last two arguments to be optional so it either works with only the first or all three. I'm hoping you can help me with this.
Thank you very much.
Adding a ? after something in a RegEx makes it optional. so something like:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))? /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4
Notice I introduced a new grouping around the 2nd and 3rd arguments, so the backreferences had to be shifted.
You might also want to put an optional / at the end, too, so it can be used just as if it pointed to a directory...
Here's how I solved the problem. It could be helpful for someone who might stumble upon this question:
RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))?$ /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4