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
Related
I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.
Learning PHP, I am playing around with mod_rewrite and CodeIgniter. I have configured my .htaccess file correctly with
RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]
I understand a bit of regex, and can appreciate what happens here. The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. So far so good.
I now want a URL pattern :
/<person-name>/at/<place>
to get translated to :
/index.php/person/list?personName=$1&place=$2
And i handle the request at my list function in the person controller. I do not understand why the following doesn't work:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]
What am i doing wrong/where is my understanding flawed? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404.
Many thanks!
It's possible that the simplest fix will fix your issue. By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." That could be breaking everything. You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on.
Check out How to make CodeIgniter accept "query string" URLs?. It seems to indicate that you can't mix and match the segment-based approach and the query string approach. Without seeing your controller code, I can't say for certain, but I'd start investigating there. You might also try:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]
which would do the same thing the general-purpose CI redirect rule below does; send the URL off to index.php as a query string for processing. You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string.
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
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.
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!