I have URLs which I need to strip and redirect the numbers for categories and products.
Category
example.com/9-bird-tables
Product
example.com/bird-tables/59-slate-roof-bird-table/
They need to be:
example.com/bird-tables/
example.com/bird-tables/slate-roof-bird-table/
So, the number appears in a different place depending on what you're viewing. I cant work out the rules and I'm running out of time. Can anyone help?. I've tried the following, but it only works for the first set of numbers, and not the second
RewriteRule ^[0-9]+-(.+?)/?$ $1 [L,R=301]
You can change your rule to this with 2 capturing groups:
RewriteRule ^(.*/)?\d+-(.+)$ $1$2 [L,R=301,NE]
Related
I am learning .htaccess
My URL string is
http://abc.bcd.com/company/abc
I do apply to redirect my page if the company name is abc, xyz etc. and my rewrite rule is
RewriteRule ^/company/(.*?)$ /hhhhh/ll/test_page.html?company_letter=$1 [L,PT]
Sometimes my url change to
http://abc.bcd.com/company/abc?locale=en
What will be query string condition to accommodate both the url and should work properly ?
I have tried this but not helping .
RewriteCond %{QUERY_STRING} ^locale=(.*)$
The rewrite condition should help me like
if(locale="something")
/hhhhh/ll/test_page.html?company_letter=abc&locale=something
else
/hhhhh/ll/test_page.html?company_letter=abc
You just need to add QSA flag in your rule:
RewriteRule ^/?company/(.*)$ /hhhhh/ll/test_page.html?company_letter=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
The query string part of the incoming URL is a very specific thing. First you should know that classical rewriteRules are not managing the query string.
So, for example, you cannot make a RewriteRule with a check for a query string parameter value. Query strings parameters could be repeted several times, could appear in any order, and are not url-decoded (the location part of the url is url-decoded when mod_rewrite works on it).
This explains why some RewriteCond are sometimes used on the %{QUERY_STRING}, it cannot be done in RewriteRule but could be tested in rewriteCond, with all the previous probelsm ( repetition, order, url-encoding, etc).
But some rewriteRule tags can be applied for query string managment. Currently your tags are [L,PT], which also be writtent [last,passthrough].
You can add a qsappend or QSA tag which explicitly tells mod_rewrite to combine the original query string and the generated one.
So with
RewriteRule ^/company/(.*?)$ /hhhhh/ll/test_page.html?company_letter=$1 [last,passthrough,qsappend]
This:
http://abc.bcd.com/company/abc
Will go to
/hhhhh/ll/test_page.html?company_letter=abc
And this:
http://abc.bcd.com/company/abc?locale=en
Will go to
/hhhhh/ll/test_page.html?company_letter=abc&locale=en
I would like to know if the following would be possible
RewriteRule ^([^/]*)/([^/]*)$ /search.php?type=$1&query=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /search.php?type=$1&query=$2&condition=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /search.php?type=$1&query=$2&page=$3[L]
As you can see the first and third row are similiar with the only difference being the name of the third parameter, the second rule would be used for pages such as
/isbn/1203910293/new
whilst the third rule would be used for pages such as where page aliases page number
/title/harry-potter/2
I know this seems quite silly considering I can just call the condition parameter, but it would clear things up in the future if used the parameter page
The third rule pattern ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ will not match
/title/harry-potter/2
because the rule requires four parts, e.g.
/title/harry-potter/2/xyz
or at least a trailing slash
/title/harry-potter/2/
Instead it will be matched by the second rule pattern, because it has three parts too, just like
/isbn/1203910293/new
If you want to match page numbers, you need to match against a rule similar to the second rule, but be more specific, like e.g.
RewriteRule ^([^/]*)/([^/]*)/(\d+)$ /search.php?type=$1&query=$2&page=$3 [L]
I am trying to make two rewrite rules which should point to different files. However, they somehow are pointing to the same PHP file.
Can somebody help take a look please?
RewriteRule ^destinations/([a-z]+)-(.*)$ /a.php?x=$1&y=$2 [NC]
This should be linked to www.example.com/destination/us-united-state
RewriteRule ^destinations/([a-z]+)-(.+)-(.+)$ /b.php?a=$1&b=$2&c=$3 [NC]
This should be linked to www.example.com/destination/us-united-state-california
A regular expression finds the longest possible match. So, ([a-z]+)-(.*)$ will match one or more a through z characters followed by a dash followed by ANYTHING.
([a-z]+)-(.+)-(.+) requires two dashes, but the above expression will match these too.
be sure that you have the correct number of dashes matched:
RewriteRule ^destinations/([a-z]+)-([^-]+)$ /a.php?x=$1&y=$2 [NC]
and rename the page to
www.example.com/destination/us-unitedstate
then
RewriteRule ^destinations/([a-z]+)-([^-]+)-([^-]+)$ /b.php?a=$1&b=$2&c=$3 [NC]
to match pages like
www.example.com/destination/us-unitedstate-california
Use an other delimiter between us and united-state and between united-state and california. There is no way you can detect if the three seperate words in www.example.com/destination/us-united-state are actually two or three 'things'. I advice using slashes as delimiter instead: www.example.com/destination/us/united-state and www.example.com/destination/us/united-state/california.
Your rules would be:
RewriteRule ^destinations/([a-z]+)/([^/]+)/?$ /a.php?x=$1&y=$2 [NC]
RewriteRule ^destinations/([a-z]+)/([^/]+)/([^/]+)/?$ /b.php?a=$1&b=$2&c=$3 [NC]
This uses [^/]+, which matches anything but the / character. This prevents a capturing group from matching something like us/united-states where it should only match us. Please note that this code is untested as I can't test on this computer.
I have a website with joomla and I need to redirect (301) some links
They are in this form (index.php?Itemid= identify them - all links that doesn't have this part shouldn't be redirected)
/index.php?Itemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
This should work
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
But the first ? (question mark) seems to cause problems.
In fact, if we suppose that the links are without the question mark
/index.phpItemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
I would use
RewriteRule ^index.phpItemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
and everything is perfect. But unfortunately real links has that question mark, and I have to find a solution.
What I have to do with that question mark?
Is the ? character escaped? try to add the NE (noescape) flag like this:
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301,NE]
The part behind the question mark is the query string. You can use RewriteCondto determine if it is not empty, and based on that make the decision to redirect.
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
Source: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
This should help you:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} Itemid
RewriteRule ^index.php(.*)$ http://www.ligurianotizie.it/archive/index.php$1 [L,R=301]
Every link containing "Itemid" will be redirected, the others not.
I have a problem that I cannot wrap my head around.
I'm using Apache and PHP
I need to get:
http://localhost.com/cat_ap.php?nid=5964
from
http://localhost.com/cat_ap~nid~5964.htm
How do I go about changing that around? I have done more simple mod rewrites but this is slightly more complicated. Can anyone give me a leg up or point me in the right direction
RewriteRule ^/cat_ap~nid~(.*)\.htm$ /cat_ap?nid=$1 [R]
The [R] at the end is optional. If you omit it, Apache won't redirect your users (it will still serve the correct page).
If the nid part is also a variable, you can try this:
RewriteRule ^/cat_ap~([^~]+)~(.*)\.htm$ /cat_ap?$1=$2 [R]
EDIT: As Ben Blank said in his comment, you might want to restrict the set of valid URLs. For example, you might want to make sure a nid exists, and that it's numerical:
RewriteRule ^/cat_ap~nid~([0-9]+)\.htm$ /cat_ap?nid=$1
or if the nid part is a variable, that it only consists of alphabetical characters:
RewriteRule ^/cat_ap~([A-Za-z]+)~([0-9]+)\.htm$ /cat_ap?$1=$2
Assuming the variable parts here are the "nid" and the 5964, you can do:
RewriteRule ^/cat_ap~(.+)~(.+).htm$ ^/cat_ap?$1=$2
The first "(.+)" matches "nid" and the second matches "5964".
If you want everything arbitrary:
RewriteRule ^/(\w+)~(\w+)~(\w+)\.htm$ $1?$2=$3 [L]
Where \w is equal to [A-Za-z0-9_]. And if you want to use this rule in a .htaccess file, remove the leading / from the pattern.