htaccess RewriteRule for found id in middle of url - regex

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]

Related

Redirect domain and page ID

I would like to redirect domain and product ID using 2 variable the first one for domain and second one for page id like:
RewriteRule ^/(.*)$/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]
Example from:
https://www.old-domain.com/folder_A/4640-product_name.html
to
https://www.new-domain.com/folder_A/5291-product_name.html
Regex for RewriteRule shouldn't start with /:
RewriteRule ^(.*)$/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]
And #Wiktor Stribiżew is right:
You have two end of string anchors in the regex. Use one or none - ^/(.*)/4640-(.*)
Fix it with:
RewriteRule ^(.*)/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]

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 regex redirect and replace

Going to show my lack of regex knowledge here but seem to be having trouble with the following:
I need to redirect the following example structure of a url:
www.example.com/blog-title-here/tags/this+is+a+long+tag+name
www.example.com/tag/this-is-a-long-tag-name
I need to redirect to just /tag and replace any instance of + with a -
You can use this recursive redirect rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]
This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name

.htaccess rewrite URL starting / redirect

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

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]