.htaccess rewrite URL starting / redirect - regex

I am looking for regular expression which would match a pattern
/catalogue/name-of-the-product-p1-1-1.php
Or
/catalogue/name-of-the-product-p1.php
And redirect to:
/name-of-the-product
The code I tried:
RewriteRule ^/catalogue/([A-Za-z0-9-]+)-p(.+)\.php /$1 [R=301]
Any help would be appreciated.

Try
RewriteRule ^/?catalogue/([A-Za-z0-9-]+?)-p([0-9]+)([^.]*)\.php$ /$1 [R]
The regex pattern "^/?catalogue/([A-Za-z0-9-]+?)-p([0-9]+)([^.]*).php$" Can match
both Request uris /catalogue/name-of-the-product-p1-1-1.php Or /catalogue/name-of-the-product-p1.php it first matchs "catalogue" litearly, and then "([A-Za-z0-9-]+?)" matchs "name-of-the-product" and saves the value as $1 for reuse in Rewrite target.

Try this regex:
^/catalogue/([A-Za-z0-9-]+)-p[0-9]+(-[0-9]+)*\.php

Related

regex with query string containing?

I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.

htaccess RewriteRule for found id in middle of url

I need get objectid from seo url e.g When I use this rule:
RewriteRule ^company/([A-Za-z0-9-]+)$ /lt?openobjectid=$1
evrything is ok when I use example.com/company/12 but if use example.com/company/12/some-text I get server error.
It is because your regex pattern is not matching example.com/company/12/some-text.
You can try this rule:
RewriteRule ^company/([a-z0-9-]+)(?:/.*)?$ lt?openobjectid=$1 [L,QSA,NC]

Rewrite rule regexp

I would like to ask you guys because I have a problem with one my rewrite rules and I can't figure it out how to write the good one.
I have this rule:
RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1
What I would like to do is redirect the user to the authenticate.php every time when someone tries to open something in the wp-content uploads dir and I would like to send the filename to the php
For example:
http://domain.tld/wp-content/uploads/2015/11/something.pdf
redirect to authenticate.php?file=something.pdf
But unfortunately my regexp is broken. Could someone help me?
Really thanks for it!
Try with that in your .htaccess:
RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
For http://domain.tld/wp-content/uploads/2015/11/something.pdf the result: http://domain.tld/authenticate.php?file=2015/11/something.pdf
Try using this regex with negative lookaheads:
^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$
The following URL will match the regex:
http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
However, a similar URL which ends in .js or .swf will not match. Hence, the following two URLs do not match the regex:
http://domain.tld/wp-content/uploads/2015/10/javascript.js
http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
You can test this regex out here:
Regex101

Apache redirect regexp: match something that is not following something

What regexp should stand for "everything that does not follow 'index.php' should point to 'index.php/$1'" ?
For example, "http://mysite/moo" should point to "http://index.php/moo" while "http://index.php/moo" should not.
I am currently using PCRE regexp (Apache docs are saying that PCRE should be used here) taking no effect: RedirectMatch (?<=index\.php)(\/.+) /index.php/$1.
What is the right one?
UPD: the point is to use mod_alias only, ommitting mod_rewrite
You can use a RedirectCond with a non-matching pattern ("!" prefix). Try:
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) index.php/$1
Or:
RewriteCond $1 !^/?index.php
RewriteRule (.*) index.php/$1
Maybe:
^/?(?!index\.php)(.+)
i.e. a negative lookahead
I only tried with a simple redirect to a static site (works):
RedirectMatch ^(?!/index\.htm$).*$ /index.htm
Based on this, the following might help you (not tried myself):
RedirectMatch ^(?!/index\.php$)(.*)$ /index.php$1
See also: Mastering Lookahead and Lookbehind

How to rewrite ALL urls while skipping certain folders?

i'm trying to rewrite my urls to goto a single php file:
RewriteRule ^dir/(?:(?!style|js).)*$ http://www.domain.com/single.php?uri=$1 [QSA]
However the exclusion of /dir/js and /dir/style isn't working as i was hoping it would...
[redirects] domain.com/dir
[redirects] domain.com/dir/jason
[redirects] domain.com/dir/jason/pete
[DOESN'T REDIRECT: GOOD] domain.com/dir/js
[DOESN'T REDIRECT: GOOD] domain.com/dir/js/*
[DOESN'T REDIRECT: BAD] domain.com/dir/json
How can I change the regular expression to match my needs?
Try to replace style|js with style\b|js\b.
Maybe RewriteCond could be of use like in
RewriteCond %{REQUEST_URI} !^/dir/(style|js)($|/)
RewriteRule ^/dir/(.*)$ http://www.domain.com/single.php?uri=$1 [QSA]
EDITED:
domain.com/dir/json doesn't redirect because it doesn't match the regex.
The reason /dir/json doesn't redirect is because js follows dir/, and your regex only matches when dir/ is not followed by either style or js. I think negative lookaheads are the wrong approach. I think what you actually want is something like:
RewriteCond %{REQUEST_URI} !^/dir/(js|style)(/.*)?$
RewriteRule ^dir/(.*)$ http://www.domain.com/single.php?uri=$1 [LQSA]
That basically means if the URL isn't ended with /js or /style (optionally with further path components underneath those dirs), then apply the redirect rule.
Either with your negative look-ahead assertion:
RewriteRule ^dir/(?!(?:style|js)(?:/|$))(.*) http://www.example.com/single.php?uri=$1 [QSA]
But that’s not so nice.
Or you just add another test on how $1 starts:
RewriteCond $1 !^(style|js)(/|$)
RewriteRule ^dir/(.*) http://www.example.com/single.php?uri=$1 [QSA]