helloworld001 Posted February 22, 2015 Share Posted February 22, 2015 (edited) Here is my .htaccess. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([0-9]+)/([a-zA-Z]+)/$ record.php?id=$1&name=$1 [L,QSA] My link to record page. <a href="record.php/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> On record.php page, I tried to get the GET parameters like this. The var dump returns string(0) "" string(0) "" . $id = $_GET['id']; $name = $_GET['name']; var_dump($id); var_dump($name); The record page does show up, but it's messed up because it uses the above GET parameters to retrive data from database. So I was wondering if you can tell me what am I doing wrong and how can I fix it? Edited February 22, 2015 by helloworld001 Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/ Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 The rewriterule you posted will not match the url for the record page. The rewriterule will need to be RewriteRule ^id/([0-9]+)/name/([a-zA-Z]+)/$ record.php?id=$1&name=$1 [L,QSA] Note, only names that contain only letters will be matched. If the user has non-letter characters their name then the rewriterule will fail again. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506399 Share on other sites More sharing options...
helloworld001 Posted February 22, 2015 Author Share Posted February 22, 2015 The rewriterule you posted will not match the url for the record page. The rewriterule will need to be RewriteRule ^id/([0-9]+)/name/([a-zA-Z]+)/$ record.php?id=$1&name=$1 [L,QSA] Note, only names that contain only letters will be matched. If the user has non-letter characters their name then the rewriterule will fail again. Ah yes that makes more sense. It's still not working though. I think it maybe because I have a slug-name for a name. For eg. <a href="record.php/id/23/name/category-name-here">Link to record page</a> So how would you take the hyphen(-) into consideration in your above rewrite rule? Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506448 Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 Include it in the the character class for the name ../name/([a-zA-Z-]+)/$ Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506450 Share on other sites More sharing options...
helloworld001 Posted February 22, 2015 Author Share Posted February 22, 2015 Include it in the the character class for the name ../name/([a-zA-Z-]+)/$ Alright so just to go over, here is the updated code. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/$ record.php?id=$1&name=$1 [L,QSA] Link to record page <a href="record.php/id/23/name/category-name-here">Link to record page</a> Having done all this, I am still getting "0" strings for the get paremeters(id, name) on record.php page. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506453 Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 Ohh dear. just realized your url is wrong still, Did not notice this earlier. It should not start with record.php. It needs to be like this <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> And change name=$1 to name=$2 in the htaccess. Otherwise the id will passed as the category name. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506454 Share on other sites More sharing options...
helloworld001 Posted February 22, 2015 Author Share Posted February 22, 2015 Ohh dear. just realized your url is wrong still, Did not notice this earlier. It should not start with record.php. It needs to be like this <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> And change name=$1 to name=$2 in the htaccess. Otherwise the id will passed as the category name. Here is update. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/$ record.php?id=$1&name=$2 [L,QSA] <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> Doing all that, now I get an error like this on record.php. And the url appears like this. http://localhost/id/2/name/category-name-here Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506455 Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 Tested this should work fine now RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ record.php?id=$1&name=$2 [L,QSA] Make sure the files record.php and .htaccess are both in the same directory. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506456 Share on other sites More sharing options...
helloworld001 Posted February 22, 2015 Author Share Posted February 22, 2015 Tested this should work fine now RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ record.php?id=$1&name=$2 [L,QSA] Make sure the files record.php and .htaccess are both in the same directory. I copied and pasted your code exactly as shown above. I still get the object not found error. My .htaccess and record.php are both in the main directory. But the html "link" is in a different folder like this. "main directory/template/link.php". Could this have something to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506464 Share on other sites More sharing options...
Ch0cu3r Posted February 22, 2015 Share Posted February 22, 2015 I have a feeling the .htaccess is not being read. Add some garbage text to the htaccess file. You should get a 500 Internal Server error when you trying to access your site. If no error is shown then Apache is not reading the htaccess file. Can you tell me how you installed Apache? Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506471 Share on other sites More sharing options...
helloworld001 Posted February 23, 2015 Author Share Posted February 23, 2015 I have a feeling the .htaccess is not being read. Add some garbage text to the htaccess file. You should get a 500 Internal Server error when you trying to access your site. If no error is shown then Apache is not reading the htaccess file. Can you tell me how you installed Apache? Actually .htaccess is being read. I know this because I have another line in there that removes the .php extention from the pages and it does show the pages without the .php. RewriteRule ^(.*)$ $1.php [L] I have Apache installed through XAMMP. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506494 Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2015 Share Posted February 23, 2015 I have another line in there that removes the .php extention from the pages and it does show the pages without the .php. Then that rewriterule is most likely interfering with your other rewriterule. Try changing your .htaccess to. I tested with the following and it worked well for me RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ test.php?id=$1&name=$2 [L,QSA] # this should always be the last rewrite rule! RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [L] Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506510 Share on other sites More sharing options...
helloworld001 Posted February 23, 2015 Author Share Posted February 23, 2015 Then that rewriterule is most likely interfering with your other rewriterule. Try changing your .htaccess to. I tested with the following and it worked well for me RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ test.php?id=$1&name=$2 [L,QSA] # this should always be the last rewrite rule! RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [L] I added the above exactly as it is. And I assume the link is still ike this. <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a> Still shows the "object not found" error. Are you sure the link is supposed to be set up that way because it comes out as http://localhost/id/8/name/fashion? I just noticed that the page name is missing before the id. Though if I do have it like this(without .php extention), it will give me an "internal server error". <a href="test/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a> And if I set it with .php extention, it will show the page, but again without the $_GET parameters. <a href="test.php/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a> Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506526 Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2015 Share Posted February 23, 2015 I added the above exactly as it is. And I assume the link is still ike this. <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a>Still shows the "object not found" error. Are you sure the link is supposed to be set up that way because it comes out as http://localhost/id/8/name/fashion Yes that is exactly what the link should look like. I do not understand why it is not working (although test.php should be record.php in the htaccess I forgot to change it back when I pasted the code) I just noticed that the page name is missing before the id Do you require the page name in the url? With my rewriterule it does not need it in the url. The rewriterule should match any url that looks like this /id/<id-here>/name/<category-name-here>. If it finds a match it will get the id and category from the url (this is what this ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ means) and pass these onto record.php as querystring parameters (this is what this record.php?id=$1&name=$2 means). If you really want the page name in the url then this should work too RewriteRule ^record\.php/id/([0-9]+)/name/([a-zA-Z-]+)/?$ record.php?id=$1&name=$2 [L,QSA] If this still does not work. Then I have no idea what else to suggest. Either you have more code in the htaccess which could be interfering or something is amiss with your XAMPP setup. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506533 Share on other sites More sharing options...
helloworld001 Posted February 23, 2015 Author Share Posted February 23, 2015 Yes that is exactly what the link should look like. I do not understand why it is not working (although test.php should be record.php in the htaccess I forgot to change it back when I pasted the code) Do you require the page name in the url? With my rewriterule it does not need it in the url. The rewriterule should match any url that looks like this /id/<id-here>/name/<category-name-here>. If it finds a match it will get the id and category from the url (this is what this ^id/([0-9]+)/name/([a-zA-Z-]+)/?$ means) and pass these onto record.php as querystring parameters (this is what this record.php?id=$1&name=$2 means). If you really want the page name in the url then this should work too RewriteRule ^record\.php/id/([0-9]+)/name/([a-zA-Z-]+)/?$ record.php?id=$1&name=$2 [L,QSA] If this still does not work. Then I have no idea what else to suggest. Either you have more code in the htaccess which could be interfering or something is amiss with your XAMPP setup. my .htaccess has exactly what you have, except for the obvious change from test to record.ph. I have followed your instruction 100% and still no go. I think it leaves to point out that something might be amiss within my xampp setup. I may have to reinstall it or test it on a live server. Thank you so much for your help though. Even though it did not work out for me, I still learned a bit more Rewrite rules. Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506536 Share on other sites More sharing options...
Ch0cu3r Posted February 24, 2015 Share Posted February 24, 2015 Looked back through this topic and may have overlooked something My .htaccess and record.php are both in the main directory. By main directory I take that as xampp's main directory which is C:/xampp/htdocs? So the contents of the htdocs folder reads as C:\xampp\htdocs | |- .htaccess |- record.php +- template | + link.php Quote Link to comment https://forums.phpfreaks.com/topic/294793-htacces-and-php-pretty-url-works-except-it-doesnt-give-the-_get-parameters/#findComment-1506626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.