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]
Related
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.
I inserted this rule in my .htaccess file:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
In this way I can get friendly urls like this:
http://localhost/mysite/london
I'd also like to use a friendly url for my contact page like so:
https://localhost/mysite/index.php?content=message to become:
https://localhost/mysite/contact
But if I insert the below rule into .htaccess...
RewriteRule ^contact/?$ index.php?content=message [NC,L]
...it doesn't work as it seems that the rule for the categories affects this rule.
In fact, if I comment out the category rule...
#RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
...the url friendly rule for the contact page works (https://localhost/mysite/contact)
So I'm looking for the possibility to exclude some parameter from the category rule to allow for a redirect in some case to another url.
Thanks for any suggestions...
Firstly you need to make sure the contact rule is placed before the more generic one so htaccess can process it first:
RewriteRule ^contact/?$ index.php?content=message [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
If you have it the other way around the generic rule will always be looked at and match before htaccess even gets to the contact rule.
Note though that the way you wrote your rules it will only match URLs such as
https://exmaple.com/contact
but not
https://exmaple.com/contact/something-else
However, looking at your more generic rule I assume this is intentional.
I have the following htaccess rule
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
For example if I want to add something or edit something the url will be like:
www.website.com/page_name/add
And that rule is applicable for all pages except for one page named portfolio.. for this page I want first to get a category value and than add or edit stuffs.
I tried under the first rule to put this rule:
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=portfolio&category=$1 [L]
To get a link like that:
www.website.com/portfolio/demos
Or
www.website.com/portfolio/desings
But always the first rule that works (consider the 2nd parameter like an action and not like an category).
Any idea for make exception for that specific page to treat what comes after like an "cat" and not like an "action"?
The L flag in HTACCESS tells Apache to ignore anything after that rule if the pattern is a match (which it is in your example).
Swap the rules around:
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=???&category=$1 [L]
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
Also, notice that in the first rule, there is no second parameter (since there's only one pattern), so I'm not sure where you intend to send the user...
First exclude portifilio from first rule so , replace this line :
RewriteCond %{REQUEST_URI} !^/portfolio
RewriteRule ^([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)$ ?page=$1&action=$2 [L]
Also in this rule :
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=$1&category=$2 [L]
you will be able match against the number only not page name , it should be like this :
RewriteRule ^portfolio/([a-zA-Z-_0-9]+)$ ?page=portfolio&category=$1 [L]
OR
RewriteRule ^(whatever)/([a-zA-Z-_0-9]+)$ ?page=$1&category=$2 [L]
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
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