I used the following code in my .htaccess file.:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
After using it, it finally allowed me to remove file extensions, my goal,
but then subdirectories no longer redirected to their respective index files,
instead showing just 'Forbidden' and '404' messages.
I then tried to make manual redirects in the .htaccess:
Redirect 301 /subfolder http://www.domain.com/subfolder/index
Which then got me stuck in redirect loops, and even after removing them in the source code, they have carried on. I should also point out that I have almost no experience in using .htaccess.
Welcome to the wonderful world of testing with permanent redirects: Make a mistake, and it continues making the error until you clear the browser cache.
First of all, remove the redirect, and clear the cache of your browser. Second of all, imagine that your rules are applied to every request, not just the requests you want to change.
The problem you identified was a request to a directory matching your rule, then getting rewritten. Requests to http://domain.com/sub/ would be rewritten to http://domain.com/sub/.php. Now that is not anything that exists. The only thing saving you from infinite recursion is your check for dots in the url.
So, how do we fix that? Well, we check if the requested file is not a directory to start of. Now we don't rewrite the url if it is a directory, and DirectoryIndex takes care of the rest. To prevent infinite recursion, you can test if the url already ends with .php, but in this case you already took care of that.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try with this redirect. It got stuck in a loop because 301 is a permanent redirect and it gets saved in the browser.
RedirectMatch 301 /subfolder(.*) //$1
Related
We are running multiple domains through the same code and we want to save their images in their respective folders. Here's what we are doing.
/images/www.domain1.com/logo.jpg
/images/www.domain2.com/logo.jpg
now, what I want to know is, is this possible in htaccess that we rewrite the urls without user suspecting anything. This is what I want that
<img src="/images/logo.jpg" />
should internally become through htaccess
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]
But my question is,
The above redirect continually loops
Can I achieve the img effect without user or admin suspecting anything?
Sincerely,
Khuram
Remove the R=301 to simply do a rewrite rather than a redirect:
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L]
The reason it's continually looping is that the 301 redirect causes a new request to be created for the url images/www.domain1.com/logo.jpg. That URL also matches your ^images/(.*)$ rule, so it is redirected again, ad infinitum.
If you really want to do a 301 redirect (I suspect you don't, but if you did), you could solve the infinite looping problem by adding some rewrite conditions to skip the redirect if the domain is already included:
RewriteCond {REQUEST_URI} !^images/www.domain1.com/(.*)$
RewriteCond {REQUEST_URI} !^images/www.domain2.com/(.*)$
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]
You definitely should not use R flag if you don't want to change URL in browser. However even without R flag your RewriteRule will loop infinitely and you will eventually get internal server error. Use RewriteRule like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^images/(.*)$ images/%{HTTP_HOST}/$1 [L,NC]
Which is using a special internal variable called {ENV:REDIRECT_STATUS} that is set to 200 once RewriteRule rule is applied successfully.
im trying to solve this issue. I have a web site with this simplified struture:
mysite.com/index.php
mysite.com/faq.php
mysite.com/url.php
mysite.com/users/some_content_here
If the user call the index page or the faq page they have to arrive to that pages. BUT if the user writes something like mysite.com/xgsfd (or any other string different than index or faq) they have to call the url.php wich recive the xgsfd string via GET and redirects the user to a particular page.
The url.php script is already done, but i have no idea how to solve the other part, im was thinking using a .htaccess file in the root directory but as you can see it will trigger a infinite loop of rederictions.
Any idea how to solve this issue? Thanks for any help!
You can use rewrite conditions to serve files, folders and symlinks if they actually exist, but otherwise rewrite to the url.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ url.php?url=$1 [QSA,L]
That way requests to faq.php or index.php will be served, while a request to mysite.com/xgsfd will end up rewritten to url.php?url=/xgsfd .
I am trying to capture a url such as
http://www.mysite.com/somepage.php?sometext=somevalue
and redirect it to.
http://www.mysite.com/index.php?page=somepage.php&sometext=somevalue
I tried searching for such .htaccess online, but couldn't find it.
Can you please help me?
I'm quite sure this is a duplicate, but I'm having a bit of an issue finding it/them [Edit: I found one, though possibly not the best example].
Anyway, this is a fairly standard problem resolved with fairly standard code:
RewriteRule ^(.*)$ index.php?get=$1 [L,QSA]
The RewriteRule captures the entire request as $1, and passes it to index.php as the page GET parameter.
The [QSA] flag on the end says to take any existing GET parameters (sometext=somevalue in your example), and add them as additional GET parameters on the new request. (The [L] flag just says that this should be the last rule executed.)
Note that this will also redirect requests for things like images or CSS files, so it's good to add the following lines directly before this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These lines say "if the request is for a file or directory that actually exists, don't process the rule." That way, requests for real files will be served directly by Apache, rather than being handled (or more likely, mishandled) by your PHP script.
RewriteRule ^(.*).php?sometext=(.*)$ index.php?page=$1.php&sometext=$2 [QSA,L] #rewrite
RewriteRule ^(.*).php?sometext=(.*)$ http://www.mysite.com/index.php?page=$1.php&sometext=$2 [R=301,L] #redirect
I'm writing a website that allows people to asses a web page's readability (Flesch-Kincaid Reading Ease, thank kind of thing).
Ideally I'd like the user to be able merely to preceed the target URL with mine (like many mirror sites do), and hey presto they can see the results.
I'm guessing it's got to be done with mod_rewrite, but I'm not sure how to write it, especially given that URLs may contain so much potential junk.
How would I say:
if request is mysite.com/anything-at-all ).
redirect to mysite.com/?site=anything-at-all
Except in cases where the request is for:
just for mysite.com/
The request is for mysite.com/ajaxresponse.php?target=something
Where the request is for about.php or loading.gif
Sadly everything that I have tried so far ends up in an redirect loop...
Many thanks,
Jack
Edit your .htaccess to have:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)$ ?site=$1 [NC]
The + in the regex will take care of the index page being left as is. Edit otherwise as you deem necessary (make it a 302 permanent redirect, etc...)
For excluding the specific pages you should add a line:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(ajax\.php|whatever\.gif) - [L]
RewriteRule ^(.+)$ ?site=$1 [NC]
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(^|&)site=[^&]
RewriteRule .+ /?site=$0 [QSA]
The first condition is to exclude requests to existing files and the second is to avoid a redirect is there already is a site URL argument.
I've got a Web site with a bunch of docs and pdfs in /foo/bar/
Directory browsing is turned off, so you have to know the specific URL/URI combo to get the document. I'd like to find a way of redirecting ALL requests to /foo/bar/ to a php page that checks their auth cookie is set and, if so, gets their file. Of course, I also need the rule not to trigger if the request is for auth_test.php, in order to prevent looping. I have been fighting with the .htacess in /foo/bar/ and I simply cannot get this to work. mod_rewrite makes regexes look clear and obvious by comparison.
Here is my current .htaccess file (in /foo/bar/):
RewriteEngine On
RewriteRule !^auth_test\.php$ /foo/bar/auth_test\.php?q=$1 [R=301,L]
This successfully works for request to /foo/bar/auth_test.php, but a request to /foo/bar/something sends me to /foo/bar/auth_test.php?q=
It's like $1 isn't even picked up. Additionally, this rule seems to be ignored if the file exists, so requests to /foo/bar/mydocumnent.doc result in the user being prompted to download the requested file. I've even tried to add
RewriteCond %{REQUEST_FILENAME} -f
to prevent this, but that doesn't seem to work. I'm totally at a loss here. Any suggestions?
In order for $1 to work you need to set a capturing group to be matched and assigned to $1
Try this
RewriteRule !^(auth_test\.php)$ /foo/bar/auth_test\.php?q=$1 [R=301,L]
I am not sure what checks you are doing inside auth_test.php but using RewriteRules and Conditions you can also perform cookie checks inside the .htaccess file.
Try either this:
RewriteCond $1 !^auth_test\.php$
RewriteRule (.*) /foo/bar/auth_test.php?q=$1 [R=301,L]
Or this:
RewriteCond %{REQUEST_URI} ^/foo/bar/(.*)
RewriteRule !^auth_test\.php$ /foo/bar/auth_test.php?q=%1 [R=301,L]