two parts....
PART 1 - I want to redirect any request to a directory that DOES NOT exist...
to a it's new location... AND and file that does not exist - to it's new location.
so: myDomain.com/myFolder (which no longer exists)
gets redirected to myDomain.com/newLocation/myFolder
and / or
myDomain.com/myFolder.htm (which does NOT exist)
gets redirected to myDomain.com/newlocation/myFolder
this is close...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^myFolder http://myDomain.com/newLocation/myFolder
But I don't necessarily want to TYPE every questionable folder name, I'd prefer to have the 'newLocation" directory checked before the request fails...on any request, not just things i explicitly name
PART 2
Can someone explain the symbols used in the htaccess - or point me to a list...
example:
RewriteCond %{REQUEST_URI} ^/$ Rewriterule ^(.*)$ %{REQUEST_URI}/
I look at that and I know there are meanings t0 ^/$ etc - smacks of RegEx (not familiar)
or this
RewriteCond %{REQUEST_FILENAME} !-f (what's the !-f mean ????)
RewriteCond %{REQUEST_FILENAME} !-d (what's the !-d mean ????)
RewriteRule (.*) index.php?_var=$1 [L] ( the $1 means ...)
I want to understand the symbols and the syntax...
For Part 1 here is the rule will need:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if requested URI is not a file and not a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# externally redirect to /newLocation/{URI}
RewriteRule ^ /newLocation%{REQUEST_URI} [L,R=301]
For answer to part 2 here is the Apache mod_rewrite official documentation
Related
I need help with .htaccess Rewrite rules.
I have a API which can be accessed over http://api.my.domain/products/all which is working fine and is returning result.
I would like to redirect users coming to http://api.my.domain/admin to admin folder.But it is not working with current rules.
I have added this to .htaccess but it is not working correctly for admin folder.
RewriteEngine On
RewriteCond %{Request_Filename} !-F
RewriteCond %{Request_Filename} !-d
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^admin admin/index.php [QSD,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ public/index.php [QSD,L]
This is the result I get when I enter http://api.my.domain/admin and is breaking all my php redirects:
http://api.drezga.hr/admin/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/admin/admin/checklogin.php
Can someone, please, tell me what I'm doing wrong? I have spent hours and I can't see it.
Aside: If /admin is a physical directory then you should be requesting /admin/ (with a trailing slash) to begin with, otherwise Apache/mod_dir will issue a 301 redirect to append the trailing slash.
RewriteCond %{Request_Filename} !-F
RewriteCond %{Request_Filename} !-d
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^admin admin/index.php [QSD,L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ public/index.php [QSD,L]
The first two conditions (RewriteCond directives) are being applied incorrectly to the first rule only. They need to apply to the last rule (the rewrite to public/index.php) then the rewrite to admin/index.php is not necessary (this should be handled by the DirectoryIndex).
There's no need for the QSD flag since you are checking that the query string is already empty - there is no query string to discard!
You should probably be using the -f operator on the condition, not -F (which uses subrequests and is consequently less efficient).
Try the following instead:
DirectoryIndex index.php
RewriteEngine On
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Optimisation (prevent additional filesystem check)
RewriteRule ^public/index\.php$ - [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . public/index.php [L]
RewriteRule ^$ public/index.php [L]
The RewriteCond %{REQUEST_FILENAME} !-d directive prevents requests for /admin/ being passed to public/index.php.
The additional RewriteRule at the end is to rewrite requests for the root directory, which would otherwise be omitted because of the condition mentioned above. This could be avoided by extending the DirectoryIndex directive instead, although this could change the behaviour if you have other directories that need to be accessible (or should not be routed to public/index.php). For example:
DirectoryIndex index.php /public/index.php
I have tried many methods but none has worked.
Blog is the name of the domain, Home is a folder in the domain and post.php is the page getting details from database.
So, in my domain, I have:
home/post.php
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
Above is the last code I used and it is not working. I'm getting a 404 error.
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
The .htaccess is located in the root of the folder.
You seem to be ignoring the home subdirectory. (Although you've referred to both home and Home in your question - note that this is case-sensistive.) And if "/post is a PHP file" then you should presumably be rewriting to post.php, not simply post?
Note that Apache does not support line-end comments, as you are using here. (At best they get ignored, at worst they trigger a 500 error.)
If the .htaccess file is located in the document root, as you suggest then you would need to write the rule as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/([\w-]+)$ home/post.php?read=$1 [L]
The \w shorthand character class is the same as [A-Za-z0-9_].
The RewriteRule pattern matches the URL-path relative to the location of the .htaccess file.
If, on the other hand, the .htaccess file is in the /home subdirectory (ie. at /home/.htaccess) then you would write the rule like this instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ post.php?read=$1 [L]
Note, there is no slash prefix on the substitution string.
I have the following .htaccess file on my website root
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /index.php?t=$1 [L]
It means that, for some links like
www.website.com/foo
www.website.com/bar
it will be internally replaced by
www.website.com/index.php?t=foo
www.website.com/index.php?t=bar
Problem is: one of this directories (say, bar) exists, and I want to be able to use the link www.website.com/bar/file.pdf, so I had to modify .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)/?$ /index.php?t=$1 [L]
I removed the line ending in !-d, which means “if the directory doesn’t exist”. But now I see
www.website.com/bar/index.php?t=bar
So how can I have both
www.website.com/bar
www.website.com/bar/file.pdf
with the first redirecting (internally) to www.website.com/index.php?t=bar and the second pointing to the right file?
You could try this if /bar/index.php exists:
RewriteEngine On
#
# rule 1
#
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . %{REQUEST_FILENAME}/index.php [QSA,L]
#
# rule 2
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /index.php?t=$1 [L]
This way if directory /bar/ exists, it's rewritten definitively by the first rule 1 to /bar/index.php. At the same time, /bar/file.pdf will not be touched as the file exists.
I'm sure this has been answered, I've been reading for a few hours now and am getting nowhere. I need this to work tonight and my brain is hurting, so I'm asking the the wonderful internet for help.
I'm trying to run all my pages through index_new.php (it all made sense when I decided to do that, I swear). I have two types of pages, static and dynamic. The dynamic pages are all the same kind of page, but with different data based on the database (MySQL). I'm trying to make these rewrites
/about => index_new.php?page=about
/installations => index_new.php?page=about
/installations/{site_id} => index_new.php?page=site&siteID={site_id}
(I'd love if about could be generic, but I don't want to push my luck) My .htaccess file is:
# enable rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
# rewrite rules
RewriteRule ^about index_new.php?page=about
RewriteRule ^/installations index_new.php?page=list
RewriteRule ^/installations/(.*) index_new.php?page=site&id=$1
When I try to go to /about or any other page, I get HTTP 404. As a note, my root is http://localhost/~user/public_html/new and the .htaccess file is there.
Provided:
The conditions in the question have to be met for each rule. (They are repeated as they are valid only for the next rewrite rule).
The .htaccess file is located at root.
You may try this instead of the rule-set in your question:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /about/?$ [NC]
RewriteRule .* /index_new.php?page=about [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/?$ [NC]
RewriteRule .* /index_new.php?page=list [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /installations/([^/]+)/? [NC]
RewriteRule .* /index_new.php?page=site&id=%1 [NC,L]
Maps silently:
http://example.com/about to
http://example.com/index_new.php?page=about
http://example.com/installations to
http://example.com/index_new.php?page=about
http://example.com/installations/Anything to
http://example.com/index_new.php?page=site&Anything
For permanent and visible redirection, replace [NC,L] with [R=301,NC,L].
NOTE:
These conditions in the question were not used as their purpose is not clear:
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"
To include them for one or several rules, try this:
# For NOT condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} !(statics|ajax) [NC]
# For positive condition, include the next line before the corresponding rewrite rule:
RewriteCond %{REQUEST_URI} (statics|ajax) [NC]
I've scavenged the web for answers to my mod_rewrite woes and I feel I'm at the end of my wits. I will have a URL like such: http://www.website.com/dashboard.php?username=stackoverflow. This url isn't the prettiest of such. So my goal is to do a few things here...
Eliminate the extension of the php file (I've been able to do this so far with the code I'll show below, but I don't know that it will stay according to the other things I need to do)
Eliminate the "www" prefix
Move the username query string (only if it's "username", I don't want to match "id" or such) directly after ".com/"
Move the php filename (without the extension) after query string.
The final URL should look like such: http://website.com/stackoverflow/dashboard or perhaps http://website.com/stackoverflow/profile.
The code I have right now which eliminates the file extension is such:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
Sadly this only fixes one of my issues and after looking at it, I'm getting the feeling there would be a better way to do this...
(1, 3, 4) As I understand you want to use pretty URLs, but backed by PHP pages. So it should be like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/profile$ /dashboard.php?username=$1 [L]
It will make http://website.com/stackoverflow/profile working as http://www.website.com/dashboard.php?username=stackoverflow
(2)
# Redirect www.site.com to site.com with 301
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]