I am displaying 404 error message if the user tries to access a specific folder struture
like
RewriteCond %{REQUEST_URI} ^/protected$
RewriteRule .* /404.cfm [NC,L]
RewriteCond %{REQUEST_URI} ^/protected/pdf$
RewriteRule .* /404.cfm [NC,L]
Is there any way to combine both
/protected/pdf and /protected
in one condition and a forward slash at the end as optional
I tried this to make last forward slash optional, like
RewriteCond %{REQUEST_URI} ^/protected/?$
but it's not working.
Try :
RewriteCond %{REQUEST_URI} ^/(protected|protected/pdf)/?$
RewriteRule .* /404.cfm [NC,L]
You can also use conditions with OR flag
RewriteCond %{REQUEST_URI} ^/protected/?$ [OR]
RewriteCond %{REQUEST_URI} ^/protected/pdf/?$
RewriteRule .* /404.cfm [NC,L]
It do not match:
RewriteCond %{REQUEST_URI} ^/protected/?$
mean match everything that start with /protected with an optional / and end.
I you want to match it you have to remove the $.
RewriteCond %{REQUEST_URI} ^/protected/?.*$
or :
RewriteCond %{REQUEST_URI} ^/protected
Or you can stay restrictive as #starkeen offers solution, it depend on your needs.
Related
I have this line here;
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
But when one of the 2 values is empty it shows an error that the page is not found. As example I did;
localhost/news/dgfgh
Is there a way to fix this?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} "/admin/"
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
The Regular Expression in your rewrite rule does not match localhost/news/dgfgh
The rewrite rule is looking for news followed by exactly two groups, followed by a trailing slash. To do what you want, you need two rules.
RewriteRule ^news/(.*)/(.*)/?$ ./news.php?type=$1&number=$2 [L]
RewriteRule ^news/(.*)/?$ ./news.php?type=$1 [L]
The first one is yours with a simple ? before the trailing slash to indicate that the trailing slash is options. The second one is for the case when you don't have the number in the url as in your example
I'm trying to get rid of some subdirectories on my website.
I tried this link Remove two subdirectories from url with mod_rewrite but it does not work with my case.
Actually i have this structure : www.mywebsite.com/fr/news/index.html and I want it to look like www.mywebsite.com/index.html.
In my root directory a have a htaccess file with the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+fr/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^fr/)^(.*)/$ /fr/$1 [L,NC]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
What would the change to do to make it work ?
Thank you in advance for your answers
I have this structure:
www.mywebsite.com/fr/news/index.html
and I want it to look like
www.mywebsite.com/index.html
Without knowing more, I'd suggest placing this in your root folder:
RewriteEngine on
RewriteRule ^[^\/]+\/[^\/]+\/(.*) http://www.mywebsite.com/$1
The regex reads as:
from the start, find one or more characters that aren't a forward slash
then a forward slash
then one or more characters that aren't a forward slash
then a forward slash
then capture all the remaining characters
I have this .htaccess code which I have put together from different sources and I am having trouble adding a couple of extra options to it.
<Limit GET POST PUT>
order deny,allow
deny from all
allow from XXX.XXX.XXX.XXX
</Limit>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/js/" [OR]
RewriteCond %{REQUEST_URI} "/images/"
RewriteRule .* - [L]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)/([a-zA-Z0-9-/_]+)$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/([a-zA-Z0-9-/_%]+)$ index.php?primary=$1&secondary=$2 [QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)$ index.php?primary=$1
Firstly I would like to add the forced use of www. in the URL. When I have tried to add this, the resulting URL becomes the output of the current RewriteRules, containing the GET variables. I believe this is because the rules work on a loop?
Another thing I would like to do is catch anything that doesnt meet the criteria of the current rules and send that to something like index.php?primary=error. The way it works now is almost perfect for my use because if there is a malformed URL or illegal character, the site is not going to even attempt to display the page. The site will create all URLs safely, so any bad URLs would be the result of experimenting in the address bar, but it would be nice to have an error page rather than a page not found.
Thanks in advance.. And sorry for the muddle-through use of RewriteRule!
Your regex are incorrect since hyphen can be unescaped only when it appears as first or last symbol in a character class, it needs to be escaped otherwise.
So instead of: [a-zA-Z0-9-/_%]
Use: [\w%/-]
Your complete set of rules:
RewriteEngine On
RewriteBase /
# add www in the URL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} ^/(css|js|images)/ [NC]
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3&quaternary=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2&tertiary=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?primary=$1&secondary=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ index.php?primary=$1 [QSA,L]
I'm having a hard time having this to work..
I have installed YOURLS wich is a PHP script to shorten urls.
In order to work, it needs to have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
No problem here.
But I also want to use a directory for image hosting that has nothing to do with the PHP script.
It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1
I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]
This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.
The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]
I need to rewrite any url on my domain that starts with **/club/***anyPage* to /page/club/anyPage.
For this, I believe I can use the following command:
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
Also, if the physical page does not exist I want it conditionally rewritten like so:
/club/$1 to /page/club/club.php?title=$1
So in this case if the user enters in mysite.com/club/New_Club they should actually be viewing /page/club/club.php?title=New_Club
Your original rules seems to work OK for it's job, but to encorporate the second requirement, something like this should do the job...
# do first rule if not file/directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^club/([^/]*)$ /page/club/club.php?title=$1 [NC]
# do second rule if not file/directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
or depending on other rules...
# prevent requests for real files / directories from rewritting at all
RewriteCond %{REQUEST_URI} -f
RewriteCond %{REQUEST_URI} -d
RewriteRule .* - [L]
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
RewriteRule ^club/([^/]*)$ /page/club/club.php?title=$1 [NC]