Example my URL
http://domain.com/search.php?s=search&w=state&t=town&c=category&p=1
s = search keyword
w = region
t = town
c = category
p = paging
What I have done for now in my .htaccess is
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /search.php?s=$1&w=$2&t=$3&c=$4&p=$5 [L]
And the URL will be
http://domain.com/search/keyword/region/town/category/1/
Question
How do I rewrite my .htaccess to make my URL can be global query.
Example s=bmw+5+series&w=&t=&c=
http://domain.com/search/bmw-5-siries/
Example s=bmw+5+series&w=&t=&c=car
http://domain.com/search/car/bmw-5-siries/
Example s=bmw+5+series&w=manchester&t=&c=car
http://domain.com/search/manchester/car/bmw-5-siries/
or other output as long in the queries term.`
This is possible?
The easiest method would be to edit the SEO URLs to prepend the search term with a character to identify the searched term.
i.e.
/search/bmw-5-series/r/Manchester/c/Car
or
/search/bmw-5-series/c/Luxury
and use the variables along with a RewriteCond to identify which RewriteRule to apply.
With your current method, it would be hard to identify if the third item (eg. car) is a town or the category or something else, unless you have fixed rules. (i.e If Town is passed then the Region will definitely be passed so you can write a different set of rules to handle each condition instead of the single rule you have now)
eg.
RewriteRule ^([^/]*)/([^/]*)/$ /search.php?s=$1&w=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)//$ /search.php?s=$1&w=$2&t=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /search.php?s=$1&w=$2&t=$3&c=$4 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /search.php?s=$1&w=$2&t=$3&c=$4&p=$5 [L]
Related
I have some problems with my URL rewriting.
I just want to change my URL paradigm's who is :
http://www.siteadress.com/index.php?//something/something to something like that :
http://www.siteadress.com/something/something
So I add this line :
RewriteRule (.?)index.php\?/(.*) /$1$2
But that does'nt work as I planned.
That just remove 1 / from my URL.
Yet in my meaning, I ask for (.?) anything optionnal index.php\?/ index.php?// (.*) and anything, no?
Thank you for your help.
It looks like you have the rule in reverse order. The first argument should be the pattern you're looking to match, the second argument being what you want to replace it to. In your case, you're looking for "/something/something" and wanting to rewrite that to "/index.php?//something/something". So you need something like:
RewriteRule ^/?(.*) /index.php?//$1
That will take anything after the first / in the path and append it to /index.php?//.
I'm a photographer, and run a website with thousands of event photos.
Over the years the path to many directories has changed, and, with a new software, the access to individual photos has changed as well.
I get the path changes covered like this (there are many more rules):
RewriteRule ^([0-9]{4})-major-generals-review/?(.*)$ /galleries/trooping-the-colour/$1-major-generals-review/$2 [R=301,NC,L]
RewriteRule ^([0-9]{4})-bcn-challenge/?(.*)$ /galleries/canal-and-waterway-events/$1-bcn-challenge/$2 [R=301,NC,L]
But I also have to replace, on top of the above, access to individual photos.
The "old" form is, to use a concrete example,
2013-bcn-challenge/index.php?`page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1`
and what I need is
galleries/canal-and-waterway-events/2013-bcn-challenge/1305241839185D24543HaraldJoergens_v1-single.php
so there are two replacements needed,
2013-bcn-challenge with /galleries/canal-and-waterway-events/2013-bcn-challenge/
(that part does work), and
index.php?page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1 with
1305241839185D24543HaraldJoergens_v1-single.php
which does not work. My code for the second replacement is
RewriteRule (.*)/index.php?page=[0-9]+/thumbnails/([-_0-9a-zA-Z]+)\.jpg.* $1/$2-single.php [R=301,NC]
on top of the other rules. Unfortunately, it seems to result in an endless loop.
To make matters worse, instead of "/thumbnails/", it could be "/cust_thumbnails/" or "/photos/", and the "autoload" part can be there or not.
Might anyone be able to help me get around the problem? Thanks a lot in advance!
Harald
The problem is that the query string (?page=...) is not part of the request URI, and so cannot be matched in the rewrite pattern.
As such, you will need to check the query string separately:
RewriteCond %{QUERY_STRING} ^page=\d+/((cust_)?thumbnails|photos)/([\w\-]+).jpg [NC]
RewriteRule ^(.*/)?index.php$ /$1%3-single.php? [R=301,NC,L]
Here, we are checking the query string for the presence of page=<num>, cust_thumbnails, thumbnails, photos, and, of course, the name of the photo.
Then, we are redirecting to /<folder-name>/<file-name>-single.php.
Note: In the expression, I have changed [0-9]+ to \d+ and [-_0-9a-zA-Z]+ to [\w\-]+ - these are both shorthand alternatives.
I have a problem with rewrite rule
my link
is www.something/group/group_id/place/groupName
for this
rewriteBase /
RewriteRule ^group/(.*)/(.*)/(.*)$ /group.php?gid=$1 [QSA,NC,L]
somet times my url may come www.something/group/group_id/groupName.
In Both cases I have to rewrite to group.php and I need only groupid. How to write rewrite rule to work in both situation?
Either use lazy quantifiers or prevent each matching group from matching the / itself. The way you have it currently, the first group will match as much as it can resulting in unwanted results.
RewriteRule ^group/(.*?)/(.*?)/(.*?)$ /group.php?gid=$1 [QSA,NC,L]
RewriteRule ^group/([^\/]*)/([^\/]*)/([^\/]*)$ /group.php?gid=$1 [QSA,NC,L]
An even better way, to allow people to leave out unnecessary parts (read: not needed to evaluate the result on the server side), you could even do something like this:
RewriteRule ^group/(\d+)(/.*)?$ /group.php?gid=$1 [QSA,NC,L]
(This is based on the assumption that your group id is a number)
Try this one:
^group/(.+)(/|/.+)*$
It matches for
www.something/group/group_id/place/groupName
www.something/group/group_id/groupName
I never used the RewriteRule, so it's not tested. And maybe if you add the "Regex" Tag to your question you'll get more answers ;-)
I have problems with how I should structure my product listing pages, products pages, webpages.
It roughly translate into this:
/bags/nicebag.html = /product.php?product=nicebag&category=bags
/nicebag.html = /product.php?product=nice_bag
/bags = productlisting.php?&category=bags
Problem is that webpages will share same URL structure as no.2 in the list
/contact.html = page.php?page=contact
The reason why it is not listed in .htaccess separatly is that webpages can have different names. And even the same page can be in multiple languages.
The reason of no. 1 and 2 is not combined, is that sometimes I just want to reference only to the product since it can be in multiple categories.
What kind of structure do you suggest?
.htaccess
# Mod rewrite enabled.
Options +FollowSymLinks
RewriteEngine on
# ---- Rules ----
# product.php (Search for category & product name)
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html?$ product.php?prod_id=$2&cid=$1 [NC,L]
# productlisting.php (Search for category)
RewriteRule ^([A-Za-z0-9-_]+)?$ productlisting.php?&cid=$1 [NC,L]
I would use the path prefix /products/ to identify the products related URLs. So:
/products/bags/nicebag.html → /product.php?product=nicebag&category=bags
/products/nicebag.html → /product.php?product=nice_bag
/products/bags → /productlisting.php?&category=bags
With such a structure you could also rewrite /products/ to /productlisting.php that then shows all products.
# product listing
RewriteRule ^products/$ productlisting.php [L]
RewriteRule ^products/([A-Za-z0-9-_]+])$ productlisting.php?category=$1 [L]
# product details
RewriteRule ^products/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$1 [L]
RewriteRule ^products/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$2&cid=$1 [L]
# other pages
RewriteRule ^([A-Za-z0-9-_]+)\.html$ page.php?page=$1 [L]
use a different suffix for different types, e.g html for products and htm for pages or something like that
/bags/nicebag.html = /product.php?product=nicebag&category=bags
/nicebag.html = /product.php?product=nice_bag
/bags = productlisting.php?&category=bags
/contact.htm = page.php?page=contact
or
/contact/page.html = page.php?page=contact
As it will be messy and cumbersome to maintain your rewriting rules in the .htaccess file, I would only put one rule in there, rewriting to something like:
/dispatch.php?request=[request]
e.g.
RewriteRule ^(.*)$ dispatch.php?request=$1 [L,QSA]
In dispatch.php, you dissect the request into its elements (path, querystring, anchor, ...) and decide where to go from there. That way, you can use code for the decision making, which will give you a lot more flexibility than just maintaining a huge list of custom rewrite mappings.
For example, you can identify product and category elements in the path by querying against your database and base the dispatch logic on the results in a more generic way.
[Pseudocode]
if (isProduct($lastPathElement)) {
// Maybe verify that leading path elements are categories ...
// Other preparations/verifications ...
// refer execution to product.php
}
elseif (isCategory($lastPathElement)) {
// Other preparations/verifications ...
// refer execution to productlisting.php
}
// ... (Checks for other specific stuff)
else {
// Static page or 404
// refer execution to page.php
}
I was facing the very same issue a few weeks ago.
Ended up defining a different structure for the "static" pages.
www.examples.com/contact/
or
www.examples.com/info/contact.html
So it can be distinguished from the "dynamic" pages.
There's pretty much no way to distinguish between www.examples.com/nicebag.html and www.examples.com/contact.html without putting non-product webpage names in .htaccess or doing some preliminary processing in the receiving php script.
As I see, the options are:
rewrite all requests to page.php and for those that don't match any of the non-product pages, include the product script
write the non-product page names to .htaccess dynamically (messy and bug-prone)
rethink the URL structure for non-product pages. Perhaps just as little as www.example.com/page/contact.html might help
I'd go for the third one, anyway.
I would recommend flat structure:
domain.com/bags
domain.com/contact
domain.com/nice-bag
how do I configure my .htaccess rewrite rules to accomodate GET requests?
Currently, /manager/page goes to: ?dept=manager&n=page however, some pages have additional GET reqs, and so this rule doesn't work:
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA]
I would need: ?dept=manager&n=page&id&etc=etc to go to: /manager/page/id/5/etc/6 however, not all pages present the same method of id input, IE. some pages used id, others catid, and others, bugid, so it's a bit difficult.
Thanks :)
UPDATED: END URL - id/5/etc/6
You just need to sepcify a different rule.
RewriteCond ${HTTP_METHOD} "GET"
RewriteRule --your rule--
If your input methods are really that varied, you should use multiple RewriteRules for each different format.