Block URLs in .htaccess using regular expression - php

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! :)

Related

How to remove ?q= from URL using .htaccess

I understand that .htaccess URL re-write questions have been asked a number of times however, I am really struggling to get these two things working in combination.
My site takes the following url and performs a wildcard search
http://www.localhost:8888/exercises/exercise?q=overehead%20squat&
I am using the following rule to remove all the %20 spaces to give me:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
Gives: http://www.localhost:8888/exercises/exercise?q=Overhead-Squat
The last thing I would like to do is remove the ?q= so the URL looks like this:
http://www.localhost:8888/exercises/exercise/overehead-squat
It is not essential to make it lowercase, however it is desirable.
Many thanks in advance.
Create a directory called "excercise" and place your code file in it (name it index.php), and place this .htaccess
RewriteEngine On
RewriteRule ^(.*) index\.php?q=$1

I need a regular expression to my htaccess

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]

Rewrite Url (URL Routing) in .htaccess

My directories are like:
http://www.mydomain.com/macbook-computer
http://www.mydomain.com/sony-computer
http://www.mydomain.com/lenovo-computer
I want to make that, if a user type computers/macbook-computer like:
http://www.mydomain.com/computers/macbook-computer
I want to display page which is : http://www.mydomain.com/macbook-computer.
My restrictions are:
1. User must type /computers (segment 1)
2. String coming from computers/ must end with "computer". (segment 2)
How can I make this achieve in my .htaccess file?
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/computers/([^-]+)-computer/? [NC]
RewriteRule .* %1-computer? [R=301,L]
Redirects permanently
http://www.mydomain.com/computers/anyname-computer with or without trailing slash.
To:
http://www.mydomain.com/anyname-computer
Strings computers and computer are assumed to be fixed, while anything is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: First folder /computers followed by /anyname-computer.
For silent mapping, remove R=301 from [R=301,L]
You need a rewrite rule in your .htaccess file, and a little regular expression magic. Something like this should do the trick
RewriteRule ^computers/macbook-computer$ http://www.mydomain.com/macbook-computer
Here's a nice online tool for checking rewrite rules
http://htaccess.madewithlove.be/

Display results in htaccess

Is there any way to display htaccess directives results in real time? For example:
RewriteCond %{DOCUMENT_ROOT}
Are there any instructions that can be placed inside htaccess to display %{DOCUMENT_ROOT} value?
I know that can be accomplished with a PHP script in the right address, but I was wondering if it's also possible inside htaccess?
Try this
RewriteEngine On
RewriteBase /
RewriteRule (.*) /$1?docroot=%{DOCUMENT_ROOT} [L,QSA]
Didn't have the possibility to test this but should work
From the Apache 2.2 documentation
In addition to plain text, the Substition string can include
back-references ($N) to the RewriteRule pattern
back-references (%N) to the last matched RewriteCond pattern
server-variables as in rule condition test-strings (%{VARNAME})
mapping-function calls (${mapname:key|default})

htaccess generic Rewrite Rule

I have the following rewrite set up:
RewriteRule r/(.*) scripts/report.php?cp=1&id=$1
It works great to redirect mydomain.com/r/123abc but it's redirecting all pages on the server that begin with an "r". Does anyone know how I can make this more specific to only redirect when the url is mydomain.com/r/ ?
I should note that I need to keep this generic so I can't include mydomain.com in the rule.
Try putting a ^/ in front of your r/(.*) rewrite rule. This would make it:
RewriteRule ^/r/(.*) scripts/report.php?cp=1&id=$1
I'm not sure if this works for you (not tested), but try it out:
# Rewrite ini
RewriteEngine on
RewriteBase /mydomain.com/
# Redirect all /mydomain.com/r/* calls to scripts/report.php
RewriteRule ^r/(.*)$ scripts/report.php?cp=1&id=$1 [QSA]
Hope that helps.
You should specify that the RegEx should match the entire string rather than just match the string. You can do this using the ^ and $ symbols:
RewriteRule ^r/(.*)$ scripts/report.php?cp=1&id=$1

Categories