Jump to content

htacces and php pretty url works except it doesn't give the $_GET parameters


helloworld001

Recommended Posts

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 by helloworld001
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

Ohh dear.  :facepalm: 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.

Link to comment
Share on other sites

Ohh dear.  :facepalm: 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.


Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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]
Link to comment
Share on other sites

 

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>
Link to comment
Share on other sites

 

 

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.