hide ID mod_rewrite - php

is there a way to hide IDs using only .htaccess and without changing my php code?
htt://www.domain.com/show.php?categoryID=2&cname=electronic&productid=21&name=laptops
to
htt://www.domain.com/electronics/laptops
thanks in advance.

In your .htaccess you can put:
RewriteEngine On
RewriteRule ^(electronics|other|categories|here)/(laptops|other|products|here)/$ shop.php?cname=$1&name=$2
Basically I made a RegEx that matches all of your products and all of your categories, and broke that out into the cname and name and passed them to show.php.
However note I didn't pass the ID numbers to your show script. Rewrite Engine can't figure those out for you, your script will need to take the names and look up the ids itself.

Related

htaccess rewrite for multiple query string with one dynamic empty query string

So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.

.htaccess url rewrites for white label sites

I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]

Setting up a bunch of short/friendly redirect URLs

I need to setup hundreds of short redirect URLs which follow this format:
http://mysite.com/shorturl
which will redirect to
http://mysite.com/index.php?id=N, where N is the ID corresponding to that shortcut.
What's the best way to do this.. should I be writing a ModRewrite line for each individually, or is there a better way to do this that incorporates storing a column in the database or a file?
Update: to clarify, the id N is an ID in the database. The shorturl is a small phrase used for linking people to the page corresponding to that ID.
Is there a reason why you didn't want to do something like making
http://mysite.com/shorturl
redirect to
http://mysite.com/index.php?id=shorturl
And index.php can look up the correct id in the database from the shorturl and do the right thing.
Obviously, to do this, all you'd need is one line in your .htaccess:
RewriteRule ^([A-z,0-9,_,-]+)/?$ index.php?id=$1 [PT]

PHP id in .htaccess

I Have a .htaccess file for a blog script which currently generates a number for a blog post based on it's ID in the MySQL database. Is there any way I can change this so that it uses something like $subject as the URL from my PHP file? I have pasted my current .htaccess code below.
RewriteEngine On
RewriteRule ^blog-([0-9]+).html$ index.php?act=blog&id=$1
I hope people can understand what I am trying to describe.
Thanks in advance.
Regards,
Callum Whyte
Using just the subject is probably a bad idea, if you ever change the title, all links to your page (from google, or elswhere) will be broken. I would include both the subject and the idea, that way you'll always be able to match an URL to a blog post:
RewriteEngine On RewriteRule ^blog-[^-]+-([0-9]+).html$ index.php?act=blog&id=$1
This should match everything except a - between blog and the id.
Now you should be able to call, e.g., *yousite/blog-some_title_or_other-494.html* and it will call index.php?act=blog&id=494. This new rule shouldn't break any of your old backlinks either. If it does, let me know and I'll fix it.

URL rewriting with PHP MySQL .htaccess & GET

I have a MySQL database with 5000 items. The table is structured as
id
product-name
content
product-url
You can access a specific product page by going to www.site.com/products/index.php?id=123. The index.php file uses PHP to GET the ID to search the MySQL database and return the appropriate product name and content.
How can I make it so instead of www.site.com/products/index.php?id=123, the URL is rewritten or redirected to www.site.com/products/product-url? If I do it with .htaccess, will the index.php file still be able to know what ID to use to look up the product-url if the ID is not in the URL? Or is there some way to map URLs with PHP and MySQL?
(I'm a noob when it comes to this stuff, so any help will be much appreciated. Thanks.)
You can use .htaccess and a RewriteRule. The following will redirect an SEO URL to index.php with $_GET['product-url']. You will need to modify your code to either lookup the product id or use the product-url directly. Both should be unique in your products table.
RewriteEngine On
RewriteBase /
# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]
Note: I've made the product_url to only allow letters, numbers, dashes and underscores. I suggest such a restriction. In addition, the trailing slash is optional. Futhermore, this will not reappend a query string. If you need that you can add the QSA flag to the RewriteRule.

Categories