I need a regular expression to my htaccess - php

I'm very new to regular expression and I need to do a redirecting on my .htacess for some urls
Some examples are:
/lentes-de-contato/9/lentes-de-contato-biofinity-coopervision
/lentes-de-contato/9/lentes-de-contato-biofinity-teste
/lentes-de-contato/9/lentes-de-contato-biofinity
/lentes-de-contato/9/biofinity
The regex needs to match the word biofinity but don't match the word coopervision. I tried several ways to build an expression that looks like this: "biofinity" AND !"coopervision" but nothing seems to work. Till now I just have:
/lentes-de-contato/([0-9]+)/(.*biofinity.*)
Could anyone help me?

Maybe, so. Rewrterule will be achieved with your condition
RewriteCond %{REQUEST_URI} biofinity
RewriteCond %{REQUEST_URI} !coopervision
RewriteRule

You can do this in RewriteRule itself using negative lookahead:
RewriteRule ^lentes-de-contato/(\d+)/(?!.*?coopervision).*?-biofinity ... [L,NC]

Related

Block URLs in .htaccess using regular expression

I would like to block a couple of URLs if they match a regular expression in the htaccess file.
These are the URLs I want to block in htaccess.
Anything that contains the following in the URL pattern:
mp4:
wp-content
phpMyAdmin
All case insensitive, please note that the "mp4:" must include the colon to match the expression.
How can I accomplish this?
Thanks!
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^.*(wp-content)|(phpMyAdmin)|(mp4:).* [NC]
RewriteRule ^(.*)$ - [F,L]
Then a Forbidden message will be displayed for urls containing such string. e.g.:
You don't have permission to access /foomp4:bar/ on this server.
I hope this is what you are looking for, example
RedirectMatch users/(.+) http://www.exapmles.com/profiles/$1 [R=301,L]
more information can be found here
https://superuser.com/questions/155139/htaccess-301-redirect-with-regular-expressions
In your case you should add multiple lines! :)

Regex - mod_rewrite - Match string after characters and run as get vars

I'm looking to find and capture the url by "/-" and run the resulting find as a single get var on the preceding page:
http://domain.com/about-us/contact/-foo/-bar
would run as:
http://domain.com/about-us/contact/?get=/-foo/-bar
I'm able to find the match with:
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*)$ ?get=$1 [L]
But have been unsuccessful returning the proper get var. This yields:
http://domain.com/?get=about-us/contact/-foo/-bar
Any help would be much appreciated!
You're matching the whole URL in the RewriteRule, instead break it up into the base and the rest since you already predetermined the URL format with RewriteCond:
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*?)(\/-.*)$ $1?get=$2 [L]
Note: There are other ways to do this without RewriteRule. However, this is most like your current code.
Ok, I found the issue to the redirect vs. rewrite I was having. Basically I need to make sure and put the file in index.php.
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*?)(\/-.*)$ $1/index.php?get=$2 [L]
Thanks, Jason for your help!

why this .htaccess regular expression is not working?

i have a regular expression as below
http://www.abc.com/signup.php?id=2
RewriteRule ^signup/([a-z]+)/([a-z]+)$ /signup.php?$1=$2 [L]
but this is not working? can any one help me.is there any thing to enable in the .htaccess file?
As you are passing also numbers, you should match them in regular expression as well:
RewriteRule ^signup/([a-z0-9]+)/([a-z0-9]+)$ /signup.php?$1=$2 [L]
You might want to include other chars as well or rather use metaclass such as \w.
After edits, try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^signup\.php$ /signup/%1 [L,R=301]
RewriteRule ^signup/([0-9]+)$ /signup.php?id=$1 [L]
That'll redirect /signup.php?id=123 to /signup/123 which will then be rewritten back to /signup.php?id=123 (but not redirected).

Url Rewriting regex

I have this rule in my .htaccess
RewriteRule ^([^/\.]+)/?$ ?page=user&id=$1 [L]
It rewrite a url like
http://sitename.ext/nickname
to
http://sitename.ext/?page=user&nickname
The problem is that with a url with dots like http://sitename.ext/nick.name.test i get a 404 error..
I'm not good with regex..
That's because it's not being rewritten. You specificaly told it to exclude ., and that's what it's doing.
Personally, I would favour something like this:
RewriteRule user/(.+) ?page=user&id=$1 [L]
If you want to match any character except a slash, the regex is [^/], since the \. will cause it also to not match dots.
Your rule should be
RewriteRule ^([^/]+)/?$ ?page=user&id=$1 [L]
You might find this site helpful.

Regular expression for htaccess URL rewrite

hiii ,
I want to rewrite the directory www.mysite.com/index.php?category=news
to www.mysite.com/news
I write this code but its doesn't work
anyone can help, please
thanks for help
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?category=$1
Thanks for all who answered my question , all the codes are work when i try to write www.mysite.com/news , but I mean when i click on a link
"a href='index.php?category=news'"link"/a" I want to be rewrite to www.mysite.com/news immediately
Just to confirm, the pattern you want to match is all letters, numbers, hyphens and forward-slashes, right?
If so, try this
RewriteRule ^([a-zA-Z0-9/-]+)$ index.php?category=$1 [QSA,L]
I think the problem may have been your ordering of the hyphen and forward-slash in the character class expression. To match hyphens, they should appear first or last in the set of characters.
Does this work?
RewriteBase /
RewriteRule (.*) index.php?category=$1 [QSA,NC,L]
try this
Options +FollowSymLinks
RewriteEngine on
RewriteRule news/ index.php?category=news
RewriteRule news index.php?category=news
RewriteRule ^(news|or|some|other|category)$ index.php?category=$1 [QSA,NC,L]

Categories