Jump to content

mod_rewrite


KYKK

Recommended Posts

So i looked at the tutorial of mod_rewritehttp://www.workingwith.me.uk/articles/scripting/mod_rewriteand instead of alice and bob in tutorial i use a and b ..so i have a.html and b.htmland have a htaccess file name a so a.htaccessand it say RewriteEngine onRewriteRule ^a.html$ b.htmland it suppose to show b.html stuff when i go to a.htmlbut when i go to a.html it still show a.html stuff...the server have mod_rewrite on .. anyone have any idea what go wrong?

Link to comment
Share on other sites

The ".htaccess" file must be called EXACTLY ".htaccess", not "something.htaccess". In other words (as I recall once reading) "it's a file with no name, and an eight character extension".

Link to comment
Share on other sites

Linux doesn't really use file extensions, a dot in Linux is a valid filename (or directory name) character like anything else. You can make a directory called "http.d" if you want to, it's fine to have a dot in any file or directory name. Sometimes files that start with a dot in Linux are considered system files, so some FTP clients have an option to hide those files. It's perfectly legal to use .htaccess as a filename in either Linux or Windows.

Link to comment
Share on other sites

ok now i see it delete what i type before...so now do i need to have a script that work for everyuser , i don't want to renew the script everytime a new user register and i need to downlaod file and upload again everytime.. what can i do?

Link to comment
Share on other sites

.... edit after you post... so do that mean i need to download and upload again every time a new user register?? are there a thing that like redirect is that what you call it... like redirect all links to user.php?user=name like that or something?O i just read the post you reply on my other thread..(.*) is that the thing?RewriteEngine onRewriteRule ^(.*).html$ user.php?user=(.*) how you put thatuser.php?user=(.*) ?????????

Link to comment
Share on other sites

RewriteRule ^(.*).html$ user.php?user=$1that would match ALL pages that end in .html though, and the match ($1) would be set to everything before that. You probably want something more like this:RewriteRule ^/users/(.*)$ /user.php?user=$1That will change a URL like this:domain.com/users/user1into this:domain.com/user.php?user=user1

Link to comment
Share on other sites

404 error .... first i put it in user file right? anyway i upload to the folder before user and inside the user andthe codes areRewriteEngine onRewriteRule ^(.*).html$ user.php?user=$1RewriteRule ^/users/(.*)$ /user.php?user=$1right? i try both apple.html and apple... for with/without users folder

Link to comment
Share on other sites

404 Error!that all it say then it is my hosting people ads...it like don't have that page...so just RewriteEngine onRewriteRule ^/users/(.*)$ /user.php?user=$1i put the file inside the user folder and outside ... to make sure it work... which one i should put? for the code shouldn't it be outside user folder?

Link to comment
Share on other sites

It should go wherever your web root is, wherever the file is that loads when you just type your domain in. The rules in .htaccess will apply to the folder it is in and all folders under it, so put it in the web root and it will apply to your entire site.

Link to comment
Share on other sites

ok so let start simplei want to make just apple to go to /user.php?user=$appleRewriteEngine onRewriteRule ^/apple $ /user.php?user=$applei want http://kywebsite.co.cc/apple to kywebsite.co.cc/users.php?user=$appleI move my users.php out one level so it easier...so RewriteRule ^/apple $ /users.php?user=$applenot working... do i need to put / in front of apple? or how i do it >?

Link to comment
Share on other sites

Remove the space after the term you're looking for (it counts the space as something you're looking for), and don't put a $ in front of a static string.RewriteRule ^/apple$ /users.php?user=appleAnd make sure that you actually have a users.php file there, if there's not one then you'll get a 404 when it tries to load it.

Link to comment
Share on other sites

um... did you make a mistake?RewriteRule ^/apple$ /users.php?user=appledon't work but thisRewriteRule ^apple$ users.php?user=appleso i use yourRewriteRule ^(.*)$ user.php?user=$1and it not working butRewriteRule ^(.*)$ index.htmlis working now there no more 404 error like everything behind kywebsite.co.cc/ will go to index.html too....so now i try RewriteRule ^(.*)$ user.php?user=$1and the first part RewriteRule ^(.*)$is working and now user.php?user=$1 i tried users.php?user=apple it work so you sure it is $1 ?____________________________________________________________________________________I just found out that now the users.php is not working

<?phpsession_start();require_once 'db.php';$sql = "SELECT * FROM `users` WHERE `name` = '{$_GET['user']}'";$result = mysql_query($sql);while($row = mysql_fetch_array($result)){echo $row['title'];echo $row['name'];echo $row['email'];echo $_GET['user'];}?>

I tried all kind of quotes on the $GET user one .... why i had it before and now not working.....

Link to comment
Share on other sites

Print $_GET['user'] to see what it is, or print out the MySQL error. It's not a good idea to use anything from $_GET, $_POST, or $_COOKIE directly in a SQL query, you'll probably want to get the value first and clean it to make sure it's not going to cause problems. But the first step is just to print it out to make sure the redirect is working like you want it to.

Link to comment
Share on other sites

we had it before lol, i think i change something and made it not working.. :) O i print the get user... and it is $red so i change the URL to users.php?user=red without the $ and it work......ok so now that problem solved...RewriteEngine onRewriteRule ^apple$ users.php?user=appleeverything right ... right? there it is not users.php?user=$appleshould there a space between apple and the $ ? ...

Link to comment
Share on other sites

That's what I was trying to explain before. The $1 means that you want to use the match for the first pattern. In an expression like this:RewriteRule ^/users/(.*)$ /user.php?user=$1That says to look for a URL that contains a "users" folder, or "/users/", with some text after that to match. Whatever matches the text after the users folder is what $1 represents, so when you say you want that to redirect to /user.php?user=$1, you're saying that you want to put whatever was after the users folder onto the user variable in the URL. So it will transform a URL like this:kywebsite.co.cc/users/testuserinto this:kywebsite.co.cc/user.php?user=testuserWhen you look at this pattern:^/users/(.*)$The ^ represents the "start", and $ represents the "end". So that pattern will match something that starts with "/users/", and anything after that until the end of the URL will be matched by the (.*) pattern. In the second pattern when you use $1, the $ symbol doesn't mean the same thing. In the first pattern it means the end of the URL, in the second pattern it's sort of like a PHP variable, except that it's a backreference in the pattern (a reference to something that was matched earlier). $1 is the first backreference, $2 is the second, etc. Putting that before a string, like $apple, doesn't mean anything. If you put a space before the $ in the first pattern, then the space will be part of the pattern (it will only match if there is a space in the URL right before the end). Also note that the ^ means that the URL has to start with a certain thing. So since the pattern says it should start with /users/ then this URL will match:kywebsite.co.cc/users/testuserbut this won't:kywebsite.co.cc/test/users/testuserBecause that URL starts with /test/ instead of /users/, and the pattern says it needs to start with /users/. You'll notice that the URL doesn't include the domain, and that's because when a browser sends a request to the server it sends the domain name and the URL in separate parts. One is the host, and one is the request URI, each in it's own header. So the browser's HTTP request includes these two headers (and others):Host: kywebsite.co.ccRequest-URI: /users/testuserThe pattern is only matching the second part, so that's why it doesn't include the domain and why it starts with /users/.So the rule I posted above should be the only rule you need. If you want to get the apple example working just for testing that's fine, but that's not the version to use (you would still have to make 1 rule for every user, instead of a rule to match any user like I posted above). Once you understand how this stuff works, you can make other rules to make your URLs look good for other things. Like for this rule:RewriteRule ^/users/([^/]*)/(.*)$ /user.php?user=$1&action=$2now you can make URLs like this:/users/testuser/gallery/users/testuser/forum/users/testuser/profileand it will map to this:/user.php?user=testuser&action=gallery/user.php?user=testuser&action=forum/user.php?user=testuser&action=profile

Link to comment
Share on other sites

o so i just copy and change the $1 to $apple without deleting the $....look at this... this workRewriteRule ^users/(.*)$ /user.php?user=$1this don'tRewriteRule ^/users/(.*)$ /user.php?user=$1so i guess this problem solve.....but if there shouldn't a / in front of users then why there can be a / in front of user.php?user=$1

Link to comment
Share on other sites

o so i just copy and change the $1 to $apple without deleting the $....
Hmm.. not really, no. That's not what I was trying to say.
so i guess this problem solve.....but if there shouldn't a / in front of users then why there can be a / in front of user.php?user=$1
What URL are you using to test with? It it like this:kywebsite.co.cc/users/xxx
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...