Jump to content

url rewriting


jasmeet

Recommended Posts

<?php
$query=mysql_query("select * from category where pid=1");
while($result=mysql_fetch_array($query)){ ?>
<a href="space1.php?name=<?php echo $result['name']; ?>"><?php echo $result['name']; ?></a>
<?php } ?>
 
 
in htaccess :
 
RewriteEngine On 
 
RewriteRule ^([a-zA-Z0-9_-]+)$ space.php?name=$1
 
 
not working...
 

 

Link to comment
https://forums.phpfreaks.com/topic/284169-url-rewriting/
Share on other sites

Mod rewrite does not change your urls. It allows you to map fake urls to real ones. You need to change the links in your code so it outouts link in the format that you set your rewriterule to.

 

So change your code (from the old url)

<a href="space1.php?name=<?php echo $result['name']; ?>"><?php echo $result['name']; ?></a>

to (the new url)

<a href="/<?php echo $result['name']; ?>"><?php echo $result['name']; ?></a>

The will output urls like site.com/name

 

The site.com/name url will match the url for the rewriterule, and so when this type of url is request mod_rwrite will actually call site.com/space1.php?name=name

Link to comment
https://forums.phpfreaks.com/topic/284169-url-rewriting/#findComment-1459563
Share on other sites

@Ch0cu3r, I think, he/she wants to apply rewriting rule from space1.php?name=variable to space.php?name=variable.

The easiest way is to get the query string after the file's extension.

Assuming, the project directory is named: project.

RewriteEngine on
RewriteBase /project/
RewriteCond %{QUERY_STRING} ^name=([a-zA-Z]*)$
RewriteRule ^space1\.php$ space.php?name=%1 [R=301,L]

Link to comment
https://forums.phpfreaks.com/topic/284169-url-rewriting/#findComment-1459570
Share on other sites

Archived

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

×
×
  • 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.