How to write rewrite rule in htaccess file - apache

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

Related

Url rewrite on Apache

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?//.

.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 only first query

i m new to .htaccess. I have many searches, nothing works well.
I wanted to convert my dynamic page to static.
test.com/?gender=pop&rows=1&page=2 to
test.com/gender/pop.html?rows=1&page=2
also work for
test.com/?gender=pop to
test.com/gender/pop.html
simply only rewrite first query. thanks
This should work:
RewriteEngine On
RewriteRule ^gender/(.+)\.html$ /\?gender=$1 [L,QSA]
PS: this will work with any gender. (EX: gender=pop, gender=male, gender=female, gender=123, ...) Please let me know if you need it to be more specific.
Why you like to keep dynamic parameters ?
Test something like this :
RewriteRule ^gender/([a-z]+)\.html\?rows\=([0-9]+)\&page\=([0-9]+)$ gender=$1&rows=$2&page=$3 [L]
I hoe this help,
Mike

How do I regex htaccess for my search query

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]

rewrite urls with GET reqs

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.