Help with mod_rewrite - mod-rewrite

I written some php code which generates the URL's of pages like article.php?id=1 etc.
Obviously id is the id of the article taken from database. In the same database table, I have id, title, and category columns.
such as:
id title category
1 hello world general
My question is that how can I rewrite the URL's using the title and category of the article instead of id. For example, I want to re-write URL
example.com/article.php?id=1
To:
example.com/general/hello-world
Thanks.

The simplest way to do that would be to have a php file named general.php and use the $_SERVER['PATH_INFO'] variable.

I suppose you're going to use an incoming rewrite rule.
One way would be to extract the category and title from the URL and use another PHP file to find the article
RewriteRule .*?\.php - [PT]
RewriteRule (.*?)/(.*) articleByCategoryAndTitle.php?category=$1&title=$2
A second approach might be to use all three parts like in
example.com/category/title-42
and then use a RewriteRule to open the article page by ID
RewriteRule (?:.*)-(\d+)$ article.php?id=$1

Related

Rewriting url with the following regular expression

I'm trying to do a mod rewrite to get this url: localhost/test/index.php?hello
I created a file for this page called hello.php and it is in the folder /test
To clarify, I have another page that has a link to my hello.php, but what is the correct url so I can display localhost/test/index.php?hello in the url when I click the link to access my hello.php page.
The following doesn't seem like it is right:
RewriteRule ^.*$ index.php?$1 [L]
Try this if you want to just do php files.
RewriteEngine on
RewriteRule (.*)\.php$ /index.php?$1 [L]
To clarify what my answer does. It gives you more friendly URLs which it sounded like what your were asking for.
So you can use localhost/hello.php and it will be internally redirect to /localhost/index.php?hello. Internally means they will never see localhost/index.php?hello and will always see localhost/hello.php in their browser.
You can do any URL and it will rewrite to a php file. e.g. localhost/index.php?newpage and you can use /localhost/newpage.php
Hope that is clearer.
EDIT
You want the reverse but I don't know how your PHP is constructed but query strings are typically field/value pairs. For example name=john, page=contact, action=hello etc. You have index.php?hello but hello has no content or value.
That's probably why you're having such a hard time even re-writing your URL. Using $_GETwould require a value.
So what I would do, is if your URL was like this using field/value pairs
index.php?action=hello
Then in the index.php file you could do something like
$action = $_GET["action"];
if($action == "hello"){
//show contents of hello, include a page or whatever
}
Once you have good URLs it would be easy to rewrite it.
So if the URL that you want shown is like
index.php?action=hello and you want to redirect it to hello.php
Your .htaccess would look like this
RewriteRule ^action=([^/]+) /$1.php [R,L]
That would redirect it to the php file. If you don't want to show the redirection and keep it an internal redirect you can remove the R flag and just keep [L].
I personally don't want the user to see really long query strings URL example. mysite.com?page=music&artist=someartist&title=sometitle
So all my URL's are rewritten to be shorter and friendlier like my original answer.
you don't need .htaccess for 2. as far as you're using GET parametr - use it in index.php:
if (isset($_GET['hello'])) include('hello.php');
this will show the contents of hello.php inside index.php

Implement friendly URLs with product name

I'm currently working on an e-commerce site with the following URL format for individual products:
examplesite.com/shop.php?sec=prod&prod=373
Where 373 is an individual product number. I would like to rewrite all the product URLs so they look like this:
examplesite.com/product-name-here
Unfortunately, some of the product names contain characters such as *, / and !, which should not be included in the URL.
I have access to everything, but limited skills, so please just assume I'm completely naive if you answer!
THANKS!
Using only unique slugs ('product-name-here') could be tricky sometimes. It's better to have something like this:
examplesite.com/373/product-name-here.html // or
examplesite.com/373-product-name-here
... or any other combination, but keep the product ID in the url. To create slugs, google for PHP slug generator
If you're running your server with Apache, you need to load the mod_rewrite module and add an .htaccess to the root of your project (there where you execute the index.php)
RewriteEngine On
Options FollowSymLinks
# examplesite.com/373/product-name-here.html
RewriteRule ^([0-9]*)/([a-zA-Z0-9+-_\.])\.html$ shop.php?sec=prod&prod=$1

Codeigniter: Deep level arguments

I think, this probably is novice question, but I am novice in CodeIgniter :)
Well here is the problem, I'm trying to make categories and subcategories (dynamically generated) for store, and the main problem is that, I could manage to set different options to main category with _remap function in my controller. But, if I am trying to get deeper, then the same _remap function applies, and I am stuck there.
For example, the main category uri is http://project.com/store/fruits/, but for the subcategory, of course - http://project.com/store/fruits/apples.
I want to apply different view to 3rd segment, and still be able to control main category (fruits) with _remap function.
I want to use one controller over and over, but I think, it must be crazy to copy and paste the same function content for all subcategories (hundreds of them, disguised).
Maybe there is some way to do that, but I can't find out how... Help here! :)
/Rob
Not sure why you would need the _remap function.
If "store" is your controller, you can set each top level category as a function inside store. What's passed (via the remaining URI) to each function would be the subcategories and those can be captured and looked up in a database to get the info you need. Something like this:
Function fruits(){
$sub1 = $this->uri->segment(3); // this will be apples, etc...
...
// if it's empty - call viewX
// else call db lookup for $sub1 data here and pass to viewY
}
Or...If you used .htaccess, you could reroute like this:
RewriteCond %{REQUEST_FILENAME} store.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/([a-zA-Z]+)/([a-zA-Z]+)$ store/someFunction/cat=$1&subcat=$2 [L]
This is for illustration purposes - it needs to be modified to work in your environment.
Thanks, jco for your effort, but actually, I found _remap() function working pretty well for my needs.
I created public _remap() function with two arguments - $first_level and $next_levels, and then I controlled everything after these $next_levels given information.

Mod Rewrite - hide .php, hide optional querystring

I figured out how to hide .php, but I need to hide any query string on the URL, and provide for there not being one.
Here's my current rule: RewriteRule ^/?([a-z]+)$ $1.php
I've searched everywhere, to no avail.
Lets say you have a page that displays items from some kind of a lookup. So the requested resource is www.example.com/page.php?display=news.
In this case you can use something like RewriteRule ^/news/$ page.php?display=news This way you can create friendly URLs for different resources which might need a query string value passed in.
Lets say you want to make this generic. So display can have values news, about, company which map to the urls /news/ /about/ /company/ then you simply change your rule to
RewriteRule ^/([^/]+)/?$ page.php?display=$1
You can also use this second method to say change your shopping cart system's URLs to a friendly one. Say your shopping cart uses a query string like - shop.php?category=1&product=10. You can convert this into a URL like shop/category/1/product/10. The rule would be
RewriteRule ^shop/category/([^/]+)/product/([^/]+)$ shop.php?category=$1&product=$2

Replacing the "X" in this url www.websitename.com/info.php?lid=X

Help please.
I am looking for the best way to replace the "X" (number) in this url
www.websitename.com/info.php?lid=x
the "x" is a numerical value - i would like to replace the "X" with the "name" field from my database.
Is mod rewrite the way to go? I have multiple urls of the same format (different "X" value of course at the end) that i wish to change to create more friendly urls by replacing the "X" with the corresponding value from the database field "name".
If mod rewrite is the way to go can anyone help out with recommended code to go in the htaccess?
Thanks in advance.
Totally edited: My previous answer was based on a misunderstanding of what you're trying to ask.
What you are asking is to create a friendly URL system. This is covered in many tutorials -- just search for "friendly URLs" and you'll find lots of resources.
Here's a summary of how it works...
To create friendly URLs for your site, you would need something like this in .htaccess (not sure if I got the RewriteRule right because this is completely off the top of my head, so google for a full-blown tutorial to verify):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule /info/(.+) /info.php?name=$1
</IfModule>
This means a request to http://www.example.com/info/foo would be rewritten to http://www.example.com/info.php?name=foo.
Then you need to modify your application (in particular, the info.php file) to handle this new request format in which the name is given in the URL instead of the id.
Note that in this example, all names (e.g., "foo") must be unique. If any two items in your database have the same name, you're going to have problems. With this in mind, you might want to add a new field to your database table, which is a unique column containing a string using only alphanumeric characters and hyphens appropriate for use in a URL (this type of string is called a slug). You will basically use this slug instead of the id for database queries. Let's say you create an item named "The Discombobulator". When this item is created in your application, it should also create a slug along the lines of "the-discombobulator" and ensure it's unique. If you create a second item also called "The Discombobulator", your app might generate a slug for it like "the-discombobulator-2".
So, when someone requests http://www.example.com/info/the-discombobulator-2, mod_rewrite changes that to http://www.example.com/info.php?name=the-discombobulator-2 and hands it to your app. Your app gets the name parameter, which is "the-discombobulator-2" and looks that up in the database's slug field, and gets the matching record.
I think this is what you are looking for:
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Resources