rewrite urls with GET reqs - apache

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.

Related

.htaccess RewriteRule - multiple replacements needed

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.

.htaccess rewrite rules changing dynamic url

I am trying to make the url's better for my website for SEO purposes.
Currently pages are displayed like this:
SITE/[number]-[page name].html
I would like to get the pages to display in a format like below.
example 1.
SITE/[page name].html
or
example 2.
SITE/[page name]-[number].html
(I would prefer to leave the page number out if possible, like example 1 but if it must be included I would like it at the end of the url, like the second example above.)
I have the following rule in the .htaccess file.
RewriteRule ^([0-9]+)-(.*)$ site_page.php?page=$1
My biggest problem is that I have tried many ways to get the rewrite to do what I need.
I am a complete newbie when it comes to rewrite.
I have changed the rewrite to the following with no joy.
RewriteRule ^(.*)-([0-9]+)$ site_page.php?page=$1
RewriteRule ^(.*)-([0-9])$ site_page.php?page=$1
RewriteRule ^(.*+)-([0-9]+)$ site_page.php?page=$1
RewriteRule ^([.*]+)-([0-9]+)$ site_page.php?page=$1
The above that I have tried was to keep the page id number, I would preferably like to get rid of the numbers altogether.
I have the following Lines in the setting.php file that controls how the website creates the relevant links. And it displays them as I would like. But I get a 404 error because I think the rewrite rule is not displaying what I expected it to display.
// non seo url
$setup_url['normal']['resource'] = "$url_base/site_page.php?page=[number]";
// seo url
$setup_url['seo']['resource'] = "$url_base/[number]-[name].html";
I have looked at so many articles on mod rewrite rules, and seem to be getting no where. Any help would be greatly appreciated.
Many thanks.
Mark.
Seems that you've forgot the .html extension in rule.
RewriteRule ^(.+)-(\d+)\.html$ site_page.php?$1=$2 [L]

How would I use mod_rewrite to turn this query string request in to a clean url?

How can I use mod_rewrite to take http://www.site.com/events?pg=4 and turn it to a clean URL, like so: http://www.site.com/events/4 ?
Thanks for the help!
Something like this should do the job:
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events?pg=$1 [L]
What it's doing is:
Turning on the Rewrite engine
Matching any request that has the form events/something
Storing the 'something' (the brackets indicate that the match should be stored)
Redirecting to the ugly form using the stored variable.
Preventing any further rules from being applied ([L])
Hope this helps.

How to write rewrite rule in htaccess file

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 ;-)

mod_rewrite ecommerce URL design

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