Please URL Rewrite in php - php

Please how can I rewrite
Could anybody please rewrite this url?
http://localhost/display_news_cat.php?news_cat_id=14&p=2
to
http://localhost/display_news_cat/14/2
Thank you

Create an .htaccess file in the site directory and add the following lines
RewriteEngine on
RewriteRule ^display_news_cat/([\d]+)/([\d]+)$ display_news_cat.php?news_cat=$1&p=$2

Afaik, this is normally accomplished with Apache .htaccess file rewrite rules.
Is you case this would look something like:
RewriteEngine on
RewriteRule ^display_news_cat/([0-9]+)/([0-9]+)$ display_news_cat.php?news_cat_id=$1&p=$2
If this doesn't work, try checking your access logs to see what's happening.

there are different ways to archive this, and it takes only 1 minute to find this out yourself using google. you could:
use an .htacces file with rewrite-rules to let the apache do the rewriting
map everything on localhost/ to an index.php, read and parse the request-string "by hand" hand show the correct site

Also you can hold it in one script use GET to retrieve the values you wish and re-create the URL with that values. I don't know if it will help you..
Anyway a .httacess file will be much more useful for you.

Related

Remove directory name from url? (example.org/folder1/file to example.org/file)

I'm looking to make a goo.gl/bit.ly etc type domain shortener for my private use using my domain. I've got the script set up to make a file etc, my only issue is that I want to have each new file be created into a new folder (redirects/file), but I want to be able to have mydomain.eu/file redirect straight to the file located in the redirects directory. It seems this would be done using the .htaccess file, but I can't get that working. If anybody could provide any advice, it'd be greatly appreciated.
(Also, if anyone could provide info on how I could automatically clear out the redirects folder say, every week, that'd be great. :D )
I'm running Apache on CentOS, if that's relevant.
Thanks in advance!
Try this in your .htaccess file.
RewriteEngine on
RewriteRule ^/(.*)$ redirects/$1 [R=301,L]

Treat directory as request for a PHP file

This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.

Total noob at mod_rewrite

I am trying to rewrite my URL and having serious issues. I am on a Godaddy linux hosting server and it didn't come with any type of config file or .htaccess file. I created my own .htaccess file with the below rewrite info but I have no idea how to do anything else:
RewriteEngine On
RewriteRule ^NSN/([^/]*)\.html$ /nsn.php?NSN=$1 [L]
The people at Godaddy told me I could use URL Redirect to help with this but I am not even sure what that means.
If someone could please help with the next steps of how to make this work, it would be greatly appreciated.
Thank You.
Rewriting URLs is mapping the URL you see in the browser from the default to something more meaningful or memorable. The simplest example is something like this:
# Replace all html references with php
RewriteRule html php
A testing tool will save time, and understanding how to use aliases or redirects to avoid regular expressions may be helpful.

shorter url with php

I've seen it before and I have no clue how to do it or what to look for on google to find it.
Basically I'd like to know how to get these shortened urls to work, what i mean is for example
this: http://website.com/index.php?id=666
turned into this: http://website.com/666
Is there some way to do this through php or do people actually go around making maps for each id?
thanks
Hi there depends what server you are on but if you are using a server with apache that has mod rewrite you can use a htaccess rule.
So an example would be:
first create a .htaccess file then paste in the following content:
RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L]

Replace articles.php?id=123 with /articles/123-title-of-article

I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks
Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.
The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.

Categories