Jump to content

Help with URL rewriting using PHP only?


Atashi

Recommended Posts

Can anyone please provide me a example and explanation of URL rewriting with PHP only.

I already tested using .htaccess and it works fine.

What I want to achieve is to rewrite a URL without using a .htaccess. 
I read some blogs but still can't understand a bit. 

 

Link to comment
Share on other sites

No .htaccess, or other means of affected Apache, means your URLs all have to point to an actual PHP file. However you can basically treat one like a directory and it will still work. For example,

/blog/post.php/2013/10/blah-blah-blah
will execute the blog/post.php script (if it exists), and from there you can look in $_SERVER at either

a) the REQUEST_URI to give you the whole path and query string ("/blog/post.php/2013/10/blah-blah-blah"), or

b) the PATH_INFO to get everything after the filename ("/2013/10/blah-blah-blah")

Link to comment
Share on other sites

Can you provide an example so I can understand what you post much better. :) thx for the responds anyway.

by the way, the url is declared thru a form
for example

 

<form action="thevery/long/url.php>

i need to change the url to shorter one. :) thx agan

Link to comment
Share on other sites

URL Rewriting isn't strictly achieved through PHP alone, it's the web server that accepts and processes the URI request that needs to know what to do with the URL it has been given by the user.

 

If you want to shorten /thevery/long/url.php to /my-custom-url you need to tell the web server how to process that.

 

If you wanted a basic example, here is something similar that WordPress uses for Apache servers

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It sends all requests to /index.php where you can then process the URL yourself, through PHP, by accessing the URI through $_SERVER['REQUEST_URI']

Link to comment
Share on other sites

URL Rewriting isn't strictly achieved through PHP alone, it's the web server that accepts and processes the URI request that needs to know what to do with the URL it has been given by the user.

 

If you want to shorten /thevery/long/url.php to /my-custom-url you need to tell the web server how to process that.

 

If you wanted a basic example, here is something similar that WordPress uses for Apache servers

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It sends all requests to /index.php where you can then process the URL yourself, through PHP, by accessing the URI through $_SERVER['REQUEST_URI']

Hi thx for the reply, I believe your example is the codes for .htaccess right? 

 

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.