Forum Moderators: phranque

Message Too Old, No Replies

Static To Dynamic URL using Modrewrite

Mod, rewrite, static to dynamic

         

kkonline

6:00 pm on Sep 19, 2012 (gmt 0)

10+ Year Member



Hi,
I have site running and wish to convert the url
http://example.com/variable

http://example.com/index.php?action=variable

This requirement is opposite to the usual method to convert dynamic url to static url

How can this be achieved with mod rewrite rules

Thanks,
Krishna Khanna

[edited by: incrediBILL at 1:40 am (utc) on Sep 20, 2012]
[edit reason] use example.com, removed sig URL, see TOS [/edit]

g1smd

6:14 pm on Sep 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use example.com in this forum.

Supply your code for review...

kkonline

9:57 am on Sep 20, 2012 (gmt 0)

10+ Year Member



Hi,
My code is

RewriteEngine On
RewriteRule ^index\.php\?data\=variable$ /variable [L]

However when I test it doesn't work as expected

phranque

11:37 am on Sep 20, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you will need a RewriteCond directive in order to inspect the query string and if you are reusing "variable" in the RewriteRule you'll want to capture that so you can refer back to it.

it doesn't work as expected

what request did you make and what response were you expecting?
what response did you get?

kkonline

11:53 am on Sep 20, 2012 (gmt 0)

10+ Year Member



The request made is
mysite.com/variable and the response is 500 Internal error

phranque

12:00 pm on Sep 20, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



IMPORTANT: Please Use Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]


do i understand this correctly?

when you request:
http://example.com/variable
you want the browser's address bar to show:
http://example.com/variable
while the server serves the content from:
http://example.com/index.php?data=variable


the response is 500 Internal error

what did the server error log say?

kkonline

12:30 pm on Sep 20, 2012 (gmt 0)

10+ Year Member



when we have the request:
http://example.com/variable/

We want the browser's address bar to show:
http://example.com/index.php?data=variable

phranque

1:26 pm on Sep 20, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



which url are you linking to on your web pages?

kkonline

1:31 pm on Sep 20, 2012 (gmt 0)

10+ Year Member



The url is http://example.com/variable/ and it's required to link to http://example.com/index.php?data=variable and display ontents

It's the othr way from usual regex.

phranque

1:40 pm on Sep 20, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you should serve the content from the url you are linking to internally.

if your content is linking to this url:
http://example.com/variable/
then you should be internally rewriting this path:
variable/
to this path:
index.php?data=variable

g1smd

6:52 pm on Sep 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



URLs are used "out there" on the web.

Filepaths and filenames (and query strings) are used "here" inside the server.

They are not at all the same thing, merely "related" by the action of your server configuration.

Use an a RewriteRule configured as an internal rewrite to tie the URL request "example.com/variable" to the internal filepath "/index.php?data=variable ".

lucy24

2:00 am on Sep 21, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The url is http://example.com/variable/ and it's required to link to http://example.com/index.php?data=variable and display ontents

It's the othr way from usual regex.

You've conflated two separate things.

#1 a redirect, where the user types or clicks something, and their address bar ends up saying something else

#2 a rewrite, where the user's address bar says something, and the page content really lives somewhere else.

Now, obviously you can do both things together. You can send the user to the place where the page content lives, so the address bar says where they "really" are. But why would you want to?

The one thing you definitely do not want to do is base your entire code on redirection. Redirects are only for people following outdated links from other sites, or using outdated bookmarks. Within your own site, link to the URL you want people to end up at.


Note scrupulous avoidance of unwarranted Capitals ;)