Question

In stackoverflow, the page url is like How to store post variables value

But when we use php to create web pages, its most likely has a url like www,somewebsite.com/mypage?someid=123

And then the content is displayed.

I want to know how to convert www.somewebsite.com/mypage?someid=123

into www.somewebsite.com/mypage/some_page_link_here_which_is_static

Anybody Have idea how to do it? I tried searching this question first, but it showed me c/c++/java linking :|

Edit:

I tried some of youtube videos, My HTACESS code is
RewriteEngine on
RewriteCond %(REQUEST_URI) find/([0-9]+)/
RewriteRule find(.*)/ /find.php?id=$1

which will turn some url like
something/find.php?id=11 into
something/find/11

The .htaccess file is in root of my website.But it still fails. Have I done something wrong?

Was it helpful?

Solution 3

Many CMS and web programming frameworks support the implementation of such user (and search engine) friendly links.

On the lowest level, you may use the URL Rewrite feature found on all servers.

If you use Apache server, you have to edit the .htaccess file; for IIS (Windows server) the web.config file.

Google for url rewrite apache or url rewrite IIS.

Update: As an example, see this solution (borrowed from from CodeIgniter). You can of course replace index.php, images etc. with whatever you want.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Update 2 - Explanation

As for a more in-depth explanation, I've found this excellent introduction/tutorial on the topic: URL Rewriting for Beginners

Hopefully this makes much more sense that the unexplained example above:)

OTHER TIPS

its called url-rewriting. for apache web server you can achieve this by using the mod_rewrite extension. Commonly developers uses an .htaccess file to provide the configuration directive to this module.

A simple .htaccess entry conducting a url-rewrite would be as follows, which sends out traffic to / a file called someother.html.

RewriteEngine on
RewriteRule ^$ /someother.html

One of the most common ways frameworks do routing is by routing all the traffic coming to a web app to a controller. And then analyzing the REQUEST_URI and conduct the routing with in php. This provide away to conduct the routing more dynamically than having to provide each rule statically on a .htaccess file.

try searching " change url with htaccess ".

edit your .htaccess file :

RewriteRule ^mypage/(A-Za-z0-9_+]) page.php?post_uri=$1

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

for example

www.somewebsite.com/mypage/dynamic-link-to-static-link

.htaccess part

RewriteRule ^mypage/(A-Za-z0-9_+]) page.php?post_uri=$1 [QSA]

page.php part

$post_uri = $mysqli->real_escape_string($_GET['post_uri']);

//then you can find out the row through the variable $post_uri

RewriteEngine on
RewriteCond %{REQUEST_URI} find/([0-9]+)$
RewriteRule  /find.php?id=$1

try this %(REQUEST_URI) => %{REQUEST_URI}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top