htacces url rewriting with rewrite condition - php

Currently, i have system which works through htaccess like need to hardcore url everytime a new product is added to the system, something like below
RewriteRule ^18th-and-19th-century$ sub_category.php?id=18th-and-19th-century
RewriteRule ^20th-century-furniture$ product.php?id=20th-century-furniture
RewriteRule ^pair-lacquer-cabints$ product_info.php?id=pair-lacquer-cabints
and also there are few static pages as,
RewriteRule aboutus aboutus.php?
RewriteRule contactus contactus.php?
the Idea here, what i want is to redirect the product info, category info to some index.php and access the static page as they are,
RewriteRule aboutus$ aboutus.php?
RewriteRule contactus contactus.php?
RewriteRule ^ index.php?
i tried this but everytime it takes me too the index.php page,i think there needs to some condition thing here but not able to understand how to go forward
Thanks in Advance

Try
RewriteEngine On
RewriteRule ^sub_category/([^/]*)$ /sub_category.php?id=$1 [L]
RewriteRule ^product/([^/]*)$ /product.php?id=$1 [L]
RewriteRule ^product_info/([^/]*)$ /product_info.php?id=$1 [L]
Output URLs will be
http://example.com/sub_category/18th-and-19th-century
http://example.com/product/20th-century-furniture
http://example.com/product_info/pair-lacquer-cabints

You have wrote the all url at same directory level, so there is no way you can write dynamic .htaccess rule. You need to add some identifier or some pattern in all different kind of url's which are going to access different php page.
RewriteRule ^18th-and-19th-century$ sub_category.php?id=18th-and-19th-century
RewriteRule ^20th-century-furniture$ product.php?id=20th-century-furniture
RewriteRule ^pair-lacquer-cabints$ product_info.php?id=pair-lacquer-cabints
RewriteRule aboutus aboutus.php
Like in above pages you can use url's in following style
Subcategory URL: www.domain.com/18th-and-19th-century
Product URL: www.domain.com/18th-and-19th-century/list or www.domain.com/18th-and-19th-century/20th-century-furniture
Product info: www.domain.com/18th-and-19th-century/20th-century-furniture/pair-lacquer-cabints
OR
Subcategory URL: www.domain.com/category/18th-and-19th-century
Product URL: www.domain.com/product/20th-century-furniture
Product info: www.domain.com/product_info/pair-lacquer-cabints
So accordingly you can define levels and write rewrite rules.
Similarly you can write URL for static pages
www.domain.com/content/aboutus
www.domain.com/content/contactus
or
www.domain.com/aboutus [if you don't use first level URL for subcategory like www.domain.com/18th-and-19th-century then you can use for static pages]
if this answer is helpful to you then i will write .htaccess rules

Related

htaccess show category, sub category and post in url

I'm currently building out a simple site but struggling with a bit of htaccess.
I want my links such as services, about, blog etc to be categories.
I then want to have sub categories from then such as ppc for services, meet the team for about and a selection of blog posts for blog.
When you click on the link such as a post within ppc you get the following URL structure:
sitename/category-name/subcategory-name/post-name
sitename/services/ppc/ppc-management
So far I have been able to get the url to show like this:
sitename/category-name/subcategory-name
or
sitename/category-name/post-name
The two success examples above show that I can get it to show category name and subcategory name together.
Or I can get category-name and post-name together.
So i am struggling to get category-name/subcategory-name/post-name
My current htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^services/(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
I'm struggling with fixing these two issues:
1) Having a dynamic url insertion before the catpost.php
-You will see in the htaccess i have fixed the the catpost rewrite rule to have services before. However, is there a way to have that change dynamically?
So that could reference to services, about etc.
2) Keeping the subcategory-name in the url when you view the post as it directs to sitename/category-name/post-name
Any help is greatly appreciate. I hope this post is clear but if it isn't please let me know so I can improve.
Well, if I were you, I'll try to deal with all these in a index.php and refer to other pages by include().
Seems that (.*)/(.*) is not allowed as the FIRST rule. If you want to rewrite services/1 to services.php?id=1 and catpost/5 to catpost.php?id=5, try this:
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([^/]+)/([^/]*)$ $1.php?id=$2 [NC,L]
[^/] stands for any char except a slash
To finish your secont task together, I tried:
RewriteRule ^([^/]+)/[^/]+/([^/]*)$ $1.php?id=$2 [NC,L]
(It doesn't look pretty. My fault.)
But it requires you to put something between the two slashes, like catpost/something/2, and seems that you'll get a 500 if you leave it empty.

htaccess rewriteRule not displaying new url

My question is how to get the URL in the RewriteRule to display when I click the link (domain.com/?brandID=1&name=Beretta).
My tests make me believe it might have something to do with index.php, but maybe not. My script is triggered by the GET variable being set, and queries the DB and includes the page that displays the results (and the "ugly" url).
Here's the .htaccess code:
# Rewrite for ?brandID=1&name=Name
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]
I want to replace domain.com/?brandID=1&name=Beretta with domain.com/pistol-brand/1/beretta
The code above displays the "ugly" url, but when I change the url to what the RewriteRule states (domain.com/pistol-brand/1/beretta), the page works.
How do I get the "new" url to display when the link is clicked?
Thanks for any assistance.
You need one more to redirect the ugly url to sef format
##1)Redirect /?brandID=foo&name=bar to //pistol-brand/foo/bar##
RewriteCond %{THE_REQUEST} /\?brandID=([^&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /pistol-brand/%1/%2? [L,R]
##2)Rewrite /pistol-brand/foo/bar to /?brandID=foo&name=bar##
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]

url re writing using htaccess in php

I am working on property site and I want to rewrite some of the url in my site. There are some urls which list the property type-wise. In the footer there is a list of property types such as hotels, privatevilla, apartments....etc.
Now the url of this page is property_list.php?propertytype_id=12 where 12 is the property id of apartment. Instead of this url I'd given apartments.
When user clicks on this link then the url should be sitename/apartments instead of showing property_list.php?propertytype_id=12. I've written the following code in my .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z]+)?$ /property_list.php?propertytype_id=$1 [NC,L]
When I click on aprtments then at that time it is showing apartments in the url and listing all apartments but the thing is it is redirecting home page and other pages also.
How do I redirect only property type related pages?
So you basically want this?
RewriteEngine On
RewriteRule ^apartments$ /property_list.php?propertytype_id=12 [NC,L]
Is this what you are looking for. So when the URL is /sitename/apartments rewrite it to /property_list.php?propertytype_id=12
RewriteEngine On
RewriteCond %{REQUEST_URI} /sitename/apartments [NC]
RewriteRule ^(.*)?$ /property_list.php?propertytype_id=12 [NC,L]

Url rewriting different name for file if empty

Hi I have a rewrites in place to add an author name so:
example.com/author/first-name-last-name
Now when someone navigates to:
example.com/author
it shows all the authors, however I would like the URL to be
example.com/authors
With an 's' appended at the end.
How can I achieve this without redirecting to an actual page called authors.php?
My current rule is:
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?authorslug=$1 [NC,L]
Probably this is what you're looking for:
RewriteEngine On
# show all authors
RewriteRule ^authors/?$ /author.php [L]
# show a particular author
RewriteRule ^author/([a-z0-9-]+)/?$ author.php?authorslug=$1 [NC,L]
Add a rule before the one you already have:
RewriteRule ^author/?$ /authors.php [L]

Possible ways to identify RewriteRule when both files are in the same folder?

Ok this maybe sound like the worst question on stackoverflow but here it is.
In a folder offers I have those files:
product.php
category.php
To display a product I type domain.com/offers/product.php?id=1 andto display a category, I type domain.com/offers/category.php?id=1
Because this url looks ugly I rewrite for example the product.php using .htaccess
RewriteEngine On
RewriteRule ^([a-z0-9]+)?$ product.php?id=$1 [NC,L]
and this gives me a domain.com/offers/1
Because product.php and category.php are in the same folder and both gets a numeric variable, that means it will not execute correctly.
So a possible way is to have a slug for category.php and keep the id for product.php and then write some code for it.
My question is, is this the only way ?
Update
I didn't tried it but what if I have the one as product.php?pid=1 and the other category.php?cid=1 ?
You will need to differentiate them in the URL. This scheme would make the URLs into:
example.com/offers/category/1
example.com/offers/product/99
Done via:
RewriteEngine On
RewriteRule ^offers/(product|category)/([a-z0-9]+)?$ $1.php?id=$2 [NC,L,QSA]
The first group (product|category) captures the destination script, which is translated to $1.php. The second group is translated to to the associated id.
Update
You don't have to have them appear as separate directories /products, /categories but you do have to have some way of differentiating them. Instead you can place a p or c at the start of the ID number:
example.com/offers/c1
example.com/offers/p99
Done via:
RewriteEngine On
RewriteRule ^offers/p([a-z0-9]+)?$ product.php?id=$1 [NC,L,QSA]
RewriteRule ^offers/c([a-z0-9]+)?$ category.php?id=$1 [NC,L,QSA]
Or you can expand them out to
You're going to need some difference in the displayed URL. Something like...
RewriteRule ^([a-z0-9]+)$ product.php?id=$1
RewriteRule ^category/([a-z0-9]+)$ category.php?id=$1

Categories