sef url with php or .htaccess - php

Hi i want to make safe urls for a website. I used php's explode function that way
$explode = explode("/",$_SERVER['REQUEST_URI']);
I then was extracting with $explode[2] etc the names inside the url and then with sql statements combined them with the db took ids, names and whatever else. Entered url was like
http://www.example.com/index.php?var1=value1&var2=value2&var3=value3
and made it
http://www.example.com/value1/value2/value3 through .htaccess.
Code there is
RewriteRule ^(.*)/(.*)/(.*)$ index.php?var1=$1&var2=$2&var3=$3 [L]
But the site's php is already written fully so i do not want to change all GET queries at url cause now are not working. Is there an easier way to do so or can you correct the code so i can use again GET functions above? I forgot to mention at url value1,value2 etc are not ids (numbers) but the corresponding names. For example if it is for a furniture the url is
www.example.com/furniture/chair/.. not www.example.com/3/24/...
Is there any other way to succeed this functionality? Thanks.

I'd say you might also be looking for something like
PHP dynamic DB page rewrite URL
Which will gather any and all variables from a URL and you can use them accordingly from there on in. It would be using htaccess and a similar logic to what your wanting to do, less the rewrite rule the way you have it now.

Related

How to make a url virtual 'directory' based on unique Ids

Lets start with an example: I want to do what Tumblr is doing. More specifically, every time you click on the 'reblog' button, the URL changes to something like "/reblog/UNIQUEID/UNIQUEID/".
Yet obviously they don't have millions of directories with html files in each.
So I was wondering, if I want to make a something similar how would I approach it?
We can get the Unique ID via PHP GET and then using JavaScript change the URL to display it as "/dir/uniqueId" instead of "/dir?=uniqueId" but that seems very cumbersome.
And if we do that, it also posses a new question: what if a user enters "/dir/uniqueId" in the URL bar... that wouldn't work because its an edited string from JavaScript.
I'm not familiar with htaccess, yet I'm pretty sure it has a huge implication on the answers.
So, how would one go about fixing this problem of terrible url syntax (?= etc) to something that looks more like directories, but fundamentally isn't?
Recap:
How does one make /dir/uniqueId work while using PHP/htaccess but the uniqueId are not directories or actual files
Here's an example...
.htaccess
RewriteEngine on
RewriteRule ^reblog/([^/\.]+)/?$ index.php?reblog=$1 [L]
index.php
if (isset($_GET['reblog'])) {
$uniqueId = $_GET['reblog'];
//Do some stuff with that $uniqueId
}
url example
http://www.somesite.com/reblog/foo123bar
You can build from this example to match your requirements. As your question stands now, it's too broad to provide a more specific answer.

Using mod_rewrite to substitute variable used in URL

I've gone through a few different questions like:
Rewrite for all URLs
Can mod_rewrite convert any number of parameters with any names?
Creating dynamic URLs in htaccess
Which helped me change one set of urls from domain.com/script2.php?d=1 to domain.com/(d), but now I'm stuck with something that I can't find an answer for. Currently, I have a set of URLs that are set up as:
domain.com/script.php?a=1
While I know how to change those URLs to domain.com/(a) this doesn't quite help me with this one because variable A is just a numerical identifier, so going from domain.com/script.php?products=1 to domain.com/1 doesn't do me a lot of good.
Instead, it's variable B which is actually the descriptor, ProductName. So what I'm trying to do is have it so that rather than domain.com/(a), I can get domain.com/(b). There is a complication. The reason that the original set up used variable A is that multiple products use the same descriptor in variable B, so I also need to include variable C which differentiates them, so I need the URL to be domain.com/(b)-(c).
Bonus! Remember how I said I had another script that I'd changed from domain.com/script2.php?d=1 to domain.com/(d)? Well, it'd be super awesome if I could set up my this current script to display not as domain.com/(b)-(c) but instead as domain.com/(d)/(b)-(c) because domain/(d) is actually the search page for this other script, so it's a really logical flow and would really simplify browsing, and would let users intuitively move between the search and the products without much work.
I have no idea if this is even possible, but any help would be appreciated.
Why not just rewrite everything back to your script file?
RewriteEngine on
RewriteBase /
RewriteRule .* index.php [L]
Will rewrite everything back to index.php. From there you can parse the $_SERVER['REQUEST_URI'] variable in PHP. From there you can decide what page to load based on the given url.
If you have any other folders in the same directory of the rewrite rule above, you can put another .htaccess file inside those that have RewriteEngine Off if you don't want them to be rewritten back to index.php. That is what you will need for a css file or site images.
Using this method, you could always do something like this.
domain.com/products/1
or, domain.com/search/blahblah

best way to url rewrite a large website

i have a large database and i would like to url rewrite. it is like
www.example.com/products.php?id=111&cat=99&f=6&hjk=4545..
So I wrote url rewrite for this code to appear as
www.example.com/men/watches/casio/latest.. like that and its works fine
The problem is that I have a large database and its nearly impossible to write all url rewrites separately.
I thought of generating .htaacess file automatically using php. Does that make sense? or whats the best solution for this..?
EDIT: For #ghoti
RewriteEngine On
RewriteRule ^/([^/]*)/check/([^/]*)/SubjectId/([^/]*)/UniversityId/([^/]*)/CourseId/([^/]*)\.html$ /dsh_course_subjects_topics.php?TopicId=$1&check=$2&SubjectId=$3&UniversityId=$4&CourseId=$5 [L]
You need a single rewrite rule to catch the pattern you are looking for and pass it to a php page. For example:
(note, all of this needs to be on the same line)
RewriteRule
^/(men|women)/([a-z]+)/([a-z-])/(latest|newest|expensive|cheapest)$
/products.php?gender=$1&product=$2&brand=$3&filter=$4
In that way, you can pass generic request on to PHP where you look at $_GET to find out what the dynamic values were, and then pull up the correct items.
If you look at StackOverflow, their URLs look like this:
^/questions/([0-9]+)/([a-zA-Z0-9-]+)$
Spend some time looking at ModRewrite documentation as well as Regular Expressions. Then think how you can positively identify your product (eg, you may need to incorporate an ID in the URL nonetheless).

.htaccess Rewrite/Redirect mod or PHP script

So currently E-commerce company I'm working for is in transition from an .NET based platform to the Magento PHP based site. The issue I'm running into is that we have 1000's of our current landing pages ranked well on search engines. There's no way for me to 301 all of these pages within the new platform - Magento, and I know I can't add 1000's of static redirects to the .htaccess file because of performance issues.
So my question is this, Is there a way to create rewrite or redirect rules for variables within our DB that generate the indexed pages URL's. If there is, performance wise would it be smarter to do this solely with the .htaccess file, just PHP Scripts or a combination of both?
Here's a sample of one of our current URLs and sample of what we'd like it to be redirected to:
Current URL
/Clear_For_Life-Aquariums_Bowls-AT-70_180-AQAQ-ct.html
Here's a breakdown of the data we use to generate the rewrite;
/Clear_For_Life = VendorName Column in DB
Aquariums_Bowls = Subcategory Column in DB
AT = VendorCode Column in DB
70_180 = PriceRange Column in DB
AQAQ = CategoryId Column in DB
Desired URL to be Redirected to;
/clear-for-life-aquariums-bowl
Any help or advice here would be greatly appreciated.
So my question is this, Is there a way to create rewrite or redirect rules for variables within our DB that generate the indexed pages URL's. If there is, performance wise would it be smarter to do this solely with the .htaccess file, just PHP Scripts or a combination of both?
This sounds like a job for RewriteMap. You'll need access to either the server or vhost config and create a map. You can use this mapping to call a script that can fetch something from your DB, then decide what the new URI needs to look like. The simplest usage of RewriteMap could be:
RewriteMap remap prg:/path/to/script
And later you can simply do:
RewriteRule ^(.*)$ /${remap:$1|$1} [L]
The /path/to/script would need to take an input like Clear_For_Life-Aquariums_Bowls-AT-70_180-AQAQ-ct.html, parse it, and correctly output clear-for-life-aquariums-bowl.
I don't believe you can change case or replace characters ('_' to '-') using mod rewrite.
Why can't you 301 all these pages?
If they are being accessed with .NET then where ever you do the routing / parse the url there should be a line of code to do the php redirect.
I say use htaccess, and write a simple utility to generate said htaccess file from a list of the 301 details.

The best PHP search engine-friendly method

What method can you recommended for creating search engine-friendly URLs? When coding in PHP that is. Ideally I would like something like:
http://www.example.com/article/523544
So it doesn't display the file it's opening (eg article.php)
It is quite necessary to generate a SEO friendly URL's so that most of the Search engines can easily index it.And the most interesting part with it that URL can easily correlate to the Page Content and the User can generate a Pretty URL as per the keywords he want to rank the page on different Search Engines(e.g. google.com,google.co.in,bing.com)
The best example to have Pretty Links is on Wordpress.It actually stores the Dynamic Page URL's in the Database itself.And when the Pretty Ur is being called,internally the htaccess is called and it redirects to the original dynamic page in the system.
Some basic tips from
Google
SEOmoz
may help you.
Some topics in SO:
mod_rewrite
nice url
Edit:
You need to place a .htaccess file in your document root that includes the following rules:
RewriteEngine on
RewriteRule ^article/([0-9]+)?$ article.php?id=$1 [L]
Make sure mod_rewrite enabled in Apache and you are allowed to use it.
If you read some questions in SO in this topic it will help you understand how mod_rewrite works.
To make your urls more search engine friendly you may want to use 'slugs' so you need to sanitize your article titles like in this url.
Ideally your URL needs to contain something about the topic of the URL. You gave the example of http://www.example.com/article/523544, where this is better than using standard query strings, it's still not ideal, as all that any search engine can see from it is that it's an article.
It's important to remember that the segment (a segment is the string between each slash) closest to the domain is the most important:
http://www.example.com/most-important/next-important/less-important/
I personally always try to use the following URL structure, and keep my page/article titles unique:
http://www.example.com/this-wonderful-article
Notice the use of dashes and not underscores, this is generally known as the preferred method. Using this method I usually generate and save the article's slug ('this-wonderful-article') in the database, and then search for that instead of an ID.
Appreciated that sometimes it's very difficult to just use slug, especially with a larger website. You may have multiple articles with the same title, or the website may have user-submitted content over which you have no control. If this is the case, you can use the ID without any worries, but just be sure to include the title of the article in the URL. Eg: http://www.example.com/this-wonderful-article/29587
If you're looking for a method of using these URLs then I'd suggest looking at some mod_rewrite tutorials. Personally I use a framework that does the majority of the legwork for me such as CodeIgniter (http://www.codeigniter.com), or you could use something like the Zend Framework or CakePHP. If you're only doing articles then it might be worth looking into a sturdy CMS like WordPress, although this depends largely on your requirements.

Categories