Remove multiple parameters and rewrite url Htaccess - php

I am trying to rewrite the following Url for SEO and userfriendly purposes using htaccess or other solution. I have searched and found many answers but none gives me multiple parameters and i am not advance at this.
mysite.com/myfile.php?dept=1&prodid=161&user=2018&ulevel=1&parentcust=1&prodname=My widget
to
mysite.com/products/mywidget
if My Widget has spaces or apostrophes i need them removed. example the product Tiger's eye widget should be tigerseyewidget or tigers-eye-widget

You don't have to solve your problem with rewrites only.
For starters, let's strip your query string:
user id & user level do not belong there - it's dangerous to have these in your query string. What if I change it in my browser to user=1&ulevel=5 - am I then suddenly the administrator with access to all area's? Instead, work with sessions and store the user id & ulevel there
Do you really need the product id as well as the product name in the query string? I'd say: drop the product name, the id should be enough information for you to retrieve the product name.
parentcust: what is that? Do you really need it in the query string? What if I change the value? Looks like something that belongs in a session as well.
After stripping that, all you're left with is myfile.php?dept=1&prodid=161 - now that's something that's much easier to rewrite!
How to rewrite is another question. There are plenty examples on SO how to rewrite a query string based request to a "pretty url". Changing Tiger's eye widget to tigers-eye-widget is also well documented.
But.. who generates these links? If you generate these links, why not turn it around and link your products to e.g. mysite.com/tigers-eye-widget.html and then in PHP map "tigers-eye-widget.html" to product id 161 & dept 1?
Your problem should really only apply to legacy URL's that people may have bookmarked or still appear in search results. But for those cases it's perfectly acceptable to redirect them instead of rewriting them. So you can keep the mapping in a database or textfile, process the request with PHP and do a 301 redirect to the new URL - that way you tell search engines to replace the old URL with the new one and you don't have to worry about complicated multiple .htaccess rewrite rules.

Related

Need to hide id in my RewriteRule in .htaccess

I have built a custom business directory in PHP and I'm making the URLs human readable.
My URL request for getting business sub directories is:
http://.../new/business-subdirectory.php?cat=air-conditioners&cid=281
So I have written a rule in my .htaccess which gives me pretty well URL
http://.../business-directory/air-conditioners/281
Since my MySQL queries working on IDs, so I have to pass the ID (281) in the Pretty URL but I don't want it in the URL.
I just want the Pretty URL to be:
http://.../business-directory/air-conditioners/
Here is my .htaccess rule:
RewriteRule ^business-directory/(.*)/(.*)/?$ new/business-subdirectory.php?cat=$1&cid=$2
One option that I have is to use sub directories names instead of IDs... But still wanna see that I have other options or not...?
Waiting for your answers or suggestions :)
Omer
As you are performing queries based on ID passed in URL. So without changing code you can not achieve what you are looking.
Best option would be to have slug value in URL, which is unique same as your ID and you need to change your queries based on that.
so if air-conditioners is having ID 281 then you need to change your query to fetch data based on category slug and not using ID.

Implementing a slug / clean URL system - general plan

I am currently working on a eCommerce style project that uses a search engine to browse 7,000+ entries that are stored in a database. Every one of these search results contain a link to a full description page. I have been looking into creating clean/slug URLs for this, my goal is if a user clicks on some search result entry the browser will navigate to a new page using the slug URL.
www.mydomain.com/category/brown-fox-statue-23432323
I have a system in place to convert a string / sentence into URL form. However, it is not clear to me what the proceeding steps are once these URL's are created. What is the general plan for implementing this system? Do the URL's need to be stored in a database? Am I suppose to be using post or get data from the search result page to create content in these full description urls?
I appreciate any suggestions!
Many thanks in advance!
Each product has a unique url associated with it in the database.
When you perform a search you just return the correct unique url.
That way you only ever work out what the url should be once, when the product is first added and that url will always relate to that one product. This is the stage you use your system to create that url
Maybe you can enlighten us as to if you are using a framework? Some frameworks (like Zend) have ini / xml files for routing. But you will still need to store the urls or at least the article slugs in a db.
Storing the entry urls in the db after they have been "searched" is necessary because you want slugs to stay the same for entries. This allows for better caching / SEO which will improve your sites usability.
Hope that helps!
Edit: Saw your question about pulling up individual articles. You will have to start by setting up a relation between your entries to urls in your database. Create a url table with url_id, and url. Then place url_id on the entry table. Then whenever someone goes to any URL search the url table for the current url, recall the url_id, and then pull the entry. At that point its just styling the page to make it look the way you want.
A common approach is to have a bijective (reversible) function that can convert a "regular" URL into a user-friendly URL:
E.g.:
www.mydomain.com/category/brown-fox-statue-23432323
<=>
www.mydomain.com/index.php?category=brown-fox-statue-23432323
Then you need not keep record of this mapping (convention vs. configuration).
Search StackOverflow for "User Friendly URL Rewriting" for information on how to achieve this automatically with Apache. This question is a good starting point.

Make slug mandatory in page URL?

I'm deciding whether or not to make the slug mandatory in order to view a submission.
Right now either of these works to get to a submission:
domain.com/category/id/1/slug-title-here
domain.com/category/id/1/slug-blah-foo-bar
domain.com/category/id/1/
All go to the same submission.
You can also change the slug to whatever you want and it'll still work as it just checks for the category, id, and submission # (in the second example).
I'm wondering if this is the proper way to do this? From an SEO standpoint should I be doing it like this? And if not, what should I be doing to users who request the URL without the slug?
The slug in the url can serve three purposes:
It can act as a content key when there is no id (you have an id,so this one doesn't apply)
When just a url is posted as a link to your site, it can let users know what content to expect because they see it in the url
It can be used by search engines as a ranking signal (Google does not use url words as a ranking signal very much right now as far as I can tell)
Slugs can create problems:
Urls are longer, harder to type, harder to remember, and often get truncated
It can lead to multiple urls for the same page and SEO problems with content duplication
I am personally not a fan of using a slug unless it can be made the content key because of the additional issues it creates. That being said, there are several ways to handle the duplicate content problems.
Do nothing and let search engines sort out duplicate content
They seem to be doing better at this all the time, but I wouldn't recommend it.
Use the canonical tag
When a user visits any of the urls for the content, they should get a canonical tag like such:
<link rel="canonical" href="http://domain.com/category/id/1/slug-title-here" />
As far as Google is concerned, the canonical tag can even exist on the canonical url itself, pointing to itself. Bing has advised against self referential canonical tags though. For more information on canonical tags see: http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
Use 301 redirects
Before canonical tags, the only way to avoid duplicate content would be with 301 redirects. Your software can examine the url path, and compare the slug to the correct slug. If they don't match, it can issue a 301 redirect that will send the user to the canonical url with the correct slug. The stack overflow software works this way.
so these urls:
domain.com/category/id/1/slug-blah-foo-bar
domain.com/category/id/1/
would redirect to
domain.com/category/id/1/slug-title-here
which would be the only url that would actually have the content.
Assuming you're not ever going to change a page's slug, I'd just set up domain.com/category/id/1/ to do a 301 redirect (permanent) to domain.com/category/id/1/slug-title-here, and any time someone enters a slug which is incorrect for that article (domain.com/category/id/1/slug-title-here-oops-this-is-wrong), also 301 them to the correct address.
That way you're saying to the search engines "I don't have duplicate content, look, this is a permanent redirect" so it doesn't harm your SEO, and you're being useful to the user in always taking them to the correct "friendly url" page.
I suggest you to make a rel=canonical meta tag.
This prevents do a redirect each time considering someone can link your page with infinte variant like this:
domain.com/category/id/1/?fakeparam=1
From a SEO standpoint, as long as your only ever linking to one version of those URL's, it should be fine as the other URL's won't even be picked up by the search engine (as nowhere links to them).
If however you are linking to all 3, then it could hurt your rankings (as it'll be considered duplicate content).
I personally wouldn't make the slug required, but I would make sure that (internally) all links would point to a URL including the slug.

Masking $_GET variables of the URL when passing pages in PHP

I'm trying to make the URLs of my site SEO and user friendly. It is basically a static corporate website but for the side menu I am passing some variables through URL to show the sub menu of main selected menu.
For example: Offshore staffing is one of the main menu items and one of its sub menu items is Programmers. When someone clicks Programmers I will pass the id of main menu and sub menu through URL to collapse all other menus and promote the opened menu.
I want to mask something like ?id=4&sid=4 at the end of every URL. Can't use hidden input element because I am modifying this site and the developer who actually built that site didn't use forms.
You're looking for using a .htaccess file to rewrite URL's. For example stackoverflow might use something like this:
RewriteEngine on
RewriteRule ^questions/([0-9]+)/([_\-\&\'\,\+A-Za-z0-9-]+)?$ questions.php?q=$1
This would make both stackoverflow.com/questsions/1234/a-title-of-a-page and stackoverflow.com/questions.php?q=1234 the same page, so on your website you would need to use the "tidy" version of the URL (the first one)
A lot more can be read into this and you can customize you're URL's to what you require. For example, a few places to read up on it include:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://www.easymodrewrite.com/
Generally a good way to do this (so that you don't have lots of ID's in your URL's) is to store a "URL friendly" name of the page (e.g. "name-of-page") in your database, then when the page is requested, just search your database for that name and you'll know what ID it relates to.
Translate the ID's to the words they stand for when writing the links, and use mod_rewrite to pass them back to PHP when they're visited, where you do a lookup based on the words, and find the ID again.
So the link becomes /offshore/programmers, then you do a lookup for the ID's of "offshore" and "programmers" and show the appropriate content.
What do you mean by "masking"? Is URL-rewriting, what you are searching for? If yes, you need to append the alias (e.g. "programmers") to the URL, instead of the GET-params. Those will be translated to GET-params via URL-rewriting and then matched to an ID via PHP.

Take Unique id and use as url for page

I am building a website for sharing links and pictures for php experience. Currently, each post has a page where users can comment on it. Every post also has an id that is stored in a database with it. What would be the easiest way to make this id the url of the post? For example:
For the question that has an id of 4a3cd5, I would want the url for that question to be post.com/posts.php/4a3cd5. thanks for your answers.
In the HTAccess page (.htaccess), write the following code:-
Options +FollowSymLinks
RewriteBase /
RewriteRule ^posts/(.*)/$ /posts.php?id=$1
So now, in the Address Space, if you write http://localhost/posts/4a3cd5, the user will be shown a page corresponding to the post ID of 4a3cd5. Internally, the URL which will get processed is this one http://localhost/posts.php?id=4a3cd5. This whole technique is being done by HTAccess, and this way of showing URLs to users is called SEF URLs.
More on the HTAccess tips & tricks can be found here.
Now in the page "posts.php", you can write all the logic which you want using the PHP GET Superglobal Array "$_GET['id']".
NP: A special note - please try to avoid this type of coding. Instead try using any of the available standard PHP MVC Frameworks.
That would involve a lot, maybe you should just have URL's like /posts.php/?post=4a3cd5.
Overall this would be much more practical. You can just GET the post variable and connect to your SQL database.

Categories