Rectangle 27 2

This rule should add a trailing slash to any URL which is not a real file/directory (which is, I believe, what you need since Apache usually does the redirect automatically for existing directories).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ $1/ [L,R=301]

In order to prevent Apache from appending the path relative to the document root, you have to use RewriteBase. So, for instance, in the folder meant to be your application's root, you add the following, which overrides the physical path:

RewriteBase /

It seems the trick to using RewriteBase is to have both a leading and a trailing slash; for example, "/foo/" instead of "/foo" or "foo/". RewriteBase was still kinda finicky for me, but ultimately it worked out. Thanks!

apache - How to do a mod_rewrite redirection to relative URL - Stack O...

apache mod-rewrite redirect
Rectangle 27 1

The simplest way that I can think of would be to use mod_rewrite (link is to a great introduction tutorial) to redirect ALL incoming requests to a single PHP script (or use a flag so that this redirect only happens if the request is for a file that does not exist), with the requested address passed as part of the query string. From there, have the PHP script look up where that request should go to, and redirect to it.

So if someone tries to open http://www.foo.com/old_page.html, the mod_rewrite would send to something like http://www.foo.com/redirect.php?page=old_page.html. redirect.php then does a database lookup to see what the new address for "old_page.html" is, and redirects to there.

301 redirect with PHP and MySQL on 404 - Stack Overflow

php mysql apache redirect http-status-code-301
Rectangle 27 2

Add this rule to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [L]

That sends the id portion of the request to index.php as the parameter id.

Will RewriteRule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [L] work if I my id is more than 1 digit. For eg, id=20 or id=100. Will all the digits of the id be sent?

@user2726883 Yes, the + part of that grouping means "one or more" of the digits.

@user2726883 It just means to stop rewriting in that iteration, L = "Last"

php - Serve HTML file if it exists, otherwise redirect to index with m...

php apache .htaccess mod-rewrite
Rectangle 27 2

The error appears to be the generic action not found error. It occurs because the apache rewrite rules explicitly redirect all request to the front controller unless there is a file extension in the url and the file exists on the file system.

The error would imply there is a request to css/images in one of you html templates, or more likely, and url() reference to "images/" in you css somehwere.

I've noticed this happen a lot with jquery ui when you move the images directory out of the css folder.

You might be able to see the offending request in Firebug or Webkit Inspector.

Indeed - he is most probably using url(images/...) somewhere in his css instead of url(/images/...)

symfony1 - Symfony: Action "css/images" does not exist - Stack Overflo...

symfony1
Rectangle 27 0

What if you used the client to do some of the work? Say you display an image in the web browser and always use src="/cache/images/foobar.jpg" and add an onerror="this.src='/images/foobar.jpg'". In mod_rewrite, send anything that goes to the /images/ dir to a script that will return and generate an image in the cache.

That still doesn't solve the problem of updating the cached images if they get updated. Are you writing a CDN service? :)

The project doesn't handle the client code, it is designed to just process requests, so this isn't an option. I noticed that other question after I posted mine. It looks like my best bet might be to use a RewriteMap with some randomized plain text to send some requests to the script.

apache - mod_rewrite to serve static cached files if they exist and if...

apache caching .htaccess mod-rewrite
Rectangle 27 0

The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:

RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]

However, the recommended way in Apache 2.5 is with FallbackResource:

FallbackResource index.php

In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:

require '../router.php';

$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);

Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.

apache - Routing page requests with PHP the right way - Stack Overflow

php apache mod-rewrite url-routing
Rectangle 27 0

Is the error page you're seeing the CodeIgniter error template or a generic server error? That error string sounds very much like you are using Apache or Nginx (or whatever main webserver you use) and its actually not even resolving to your CodeIgniter app at all, but searching for a PHP file that doesn't exist. You'll probably need to use mod_rewrite or something like that to make that URL point at the CodeIgniter install.

Otherwise, your implementation doesn't look completely wrong: you probably need to make sure main.php is the default route as well.

Thats probably it, I believe we use Nginx. Like I said I'm not the backend web guy here so I'm not all too familiar with our webserver or even codeigniter for that matter. I'll have to discuss this with our lead web developer tomorrow and see if he can figure out why it's pointing to a file that doesn't exist. Thanks for your help!.

Actually there was a line of code in the My_Controller file that was forcing it to use the first segment of the uri as a class name, I pulled that out and it works fine now.

php - Codeigniter Dynamic Routing to single controller - Stack Overflo...

php codeigniter content-management-system custom-routes
Rectangle 27 0

It works fine on my own server, so there are no errors in there... Perhaps mod_headers is not enabled? Or maybe you used some strange encoding that Apache does not understand? What if you just copy-paste this into a new file and overwrite the existing one?

mod_headers

apache - Internal server error (500) with a simple .htaccess file - St...

apache .htaccess
Rectangle 27 0

I finally solved this! The "File does not exist" errors were caused by browser requests for favicon.ico!

When the URL has only one segment such as http://dev.bonesphp.com/about, the browser requests the favicon from the website's root directory, which is exactly where my favicon is located. However, if the URL has two segments such as http://dev.bonesphp.com/about/test, the browser requests it from a sub-directory. In the case of the above mentioned URL it looks for the favicon in http://dev.bonesphp.com/about. This results in an 404 since there's no directory called 'about' ('/about' maps to a controller file called About.php).

After searching on Google I found an Apache rewrite rule that works for favicons requested from any directory:

http://perishablepress.com/redirect-all-requests-for-a-nonexistent-file-to-the-actual-file/

What a PIA! I'm so glad this is over!

apache - How to fix "file not found" errors in apache2 log - Stack Ove...

apache mod-rewrite file-not-found
Rectangle 27 0

Check to see if the files referenced in your logs are files that you think exist or should exist on your site. If the files are files that you think should be on your site, then you might have some incorrect links in some of you pages. You can use a web crawler to check this.

If the pages referred to in the messages are things like ".../cmd.exe" or other pages that are not and never were on your site, then these log entries probably represent probes from bots looking for known exploits in some versions of some web servers. There is not much you can do about this. Welcome to the Internet.

If the client address for many of the requests is the same over a period of an hour or more, you are best advised to block that particular IP address at your external firewall.

i have exactly this, in my log: "[object Object]". There are nothing in my site with name like this. I've blocked bots already, but this is something else. I see, that this error is from my clients (i see the session id in the ref.url) and those clients aren't bots.

http status code 404 - What can I fix to avoid this error in apache er...

apache http-status-code-404
Rectangle 27 0

#First check if the request is for a JPG:
RewriteCond %{REQUEST_URI} (.*).jpg$

#If so, see if it exists in /cache/photos/600x500/xxx.jpg
RewriteCond %{DOCUMENT_ROOT}/cache/photos/600x500/%1.jpg -f [NC]

#If so, serve that:
RewriteRule .* cache/photos/600x500/%1.jpg [L]

# Similar set of rules for other cache:
RewriteCond %{REQUEST_URI} (.*).jpg$
RewriteCond %{DOCUMENT_ROOT}/cache/photos/200x400/%1.jpg -f [NC]
RewriteRule .* cache/photos/200x400/%1.jpg [L]

# Final rewrite for jpg files:
RewriteCond %{REQUEST_URI} (.*).jpg$
RewriteRule (.*) createimage.php?image=$1 [L]

apache - How to run a script if image file does not exist with mod_rew...

image apache caching mod-rewrite
Rectangle 27 0

Multiviews is the option to look for files in other directories if it is not found in the current one. That implies that it finding results is not restricted to the same directory, not that it circumvents mod_rewrite. It uses mod_negotiation. Using the option Multiviews let the server choose, based on the pattern what is the best file to chose. That is not the situation imo you are after when using .htaccess and mod_rewrite. I never use it.

What I noticed are the RewriteConds in your .htaccess. They are not required. I got it working without them. The more you specify in .htaccess, the more problems you can have. (Rewrite can be voodoo indeed.) I made a .htaccess file without those conditions and it worked:

Options +ExecCGI
AddHandler cgi-script .cgi
RewriteEngine on
RewriteBase /StackoverflowQuestions/tests/cgi/
RewriteRule ^(.*)\.txt$ $1.cgi [L]

That is enough to make it working. Now is the RewriteRule resolving to the same filename only another extension and not filename + extension. In the handler tag [H...] do you need to specify the mime-type of the cgi file. That is application/x-httpd-cgi, not cgi-script. But you do not need to specify the H-flag when you use AddHandler.

RewriteCond
http://localhost/~posita/rewritecgi/test1/other.txt
[H=cgi-script]
[H=application/x-httpd-cgi]
404
http://localhost/~posita/rewritecgi/test1/test.txt
MultiViews
mod_rewrite
+MultiViews
rewrite_log
127.0.0.1 - - [16/Mar/2015:15:35:47 --0700] [localhost/sid#7fb378812308][rid#7fb37889aea0/subreq] (1) [perdir /.../posita/public_html/rewritecgi/test3/] pass through /.../posita/public_html/rewritecgi/test3/test.txt.cgi

If you remove -MultiViews from your first list of options in your first .htaccess, then will you not have any 404 anymore. That is how it worked at least on my server.

My remark about the [H=...] is not that it should work, but that if you read the documentation, that you should make use of the mime-type. cgi-script Is not the mime-type of a cgi-file. In my previous comment should I have said remove -Multiviews from the options list and then will you not have any internal server error due to the disabled RewriteConds.

apache - Understanding `mod_rewrite`: internally redirect `foo.txt` to...

apache .htaccess mod-rewrite cgi
Rectangle 27 0

The error appears to be the generic action not found error. It occurs because the apache rewrite rules explicitly redirect all request to the front controller unless there is a file extension in the url and the file exists on the file system.

The error would imply there is a request to css/images in one of you html templates, or more likely, and url() reference to "images/" in you css somehwere.

I've noticed this happen a lot with jquery ui when you move the images directory out of the css folder.

You might be able to see the offending request in Firebug or Webkit Inspector.

Indeed - he is most probably using url(images/...) somewhere in his css instead of url(/images/...)

symfony1 - Symfony: Action "css/images" does not exist - Stack Overflo...

symfony1
Rectangle 27 0

You set a RewriteBase but not correctly. Moreover, your rule internally rewrites to root file /track.php (which does not exist) since you put a leading slash (and so, cancels RewriteBase action).

track
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /track/

RewriteRule ^[^/]+\.png$ track.php [L]

Half of the script is working. I mean, now when I access aaaa.png the .htaccess is trying to redirect me to track.php but it can't find it. track.php and aaaa.png are in the same folder (site.tk/track)

Weird behaviour since the code i gave you takes track folder into consideration. Try to wait a bit or refresh (maybe clear your browser's cache)

Maybe there is something wrong with the path to track.php. Is there needed to tell .htaccess that track.php is inside track?

That's what RewriteBase does. Even without it, this should be working since png path does not contain any subdirectory. Requesting /track/something.png should go to /track/track.php

apache - mod_rewrite redirect from .png to .php - Stack Overflow

php apache .htaccess mod-rewrite
Rectangle 27 0

Enabling custom permalinks on WordPress with Nginx is bit tricky but easy. If you are using Apache with mod_rewrite enabled, WordPress will automatically add the the rewrite rules to your .htaccess file.

But, with Nginx you need to define the rewrite rules as well as add the try_files directive to the Nginx configuration file for your domain/website. Otherwise, if you have enabled the custom permalinks on WordPress and you are using Nginx, then clicking the post URL will return a page with 404 Error.

wordpress - 404 error for category permalinks in subfolder multisite -...

wordpress permalinks
Rectangle 27 0

The error message tells you exactly what is going on: there is nothing to match the root URL, /. That should be obvious from your urls.py: you have patterns for "admin", "index", "search-form" and "search", but nothing for "".

However you should have diagnosed this before deploying. When you ran it on your local machine with the development server, what happened when you went to localhost:8000/? You would have seen exactly the same error, and you would not then have thought it had anything to do with your Apache configuration.

When running the Django development server using the command $ python manage.py runserver the django project can be successfully reached via the url 127.0.0.1:8000/index I have performed this on both my local machine and the live server. The problem is I cannot get Apache to run. I need Apache since I want to connect my domain 'mydomain.com' to this webproject and not have it only running internally.

Well I'm now completely confused about what actual error you're seeing. You need to post the full error message, either from the error screen you see or the log file.

This is the Apache error-log Error '[Fri Jul 04 10:46:17 2014] [error] [client ] File does not exist: /var/www/mydomain.com/htdocs/DataAnalysis/DataAnalysisSite/wsgi.py/index`

I added my directory structure and the Apache Error I am receiving. I hope this makes the context of my problem a little clearer :-)

File does not exist error in django,mod_wsgi and apache configuration ...

django apache mod-wsgi
Rectangle 27 0

Did you unpack that Apache JAR and add the .class files to your application? Why didn't you just add the JAR to your CLASSPATH?

You should not be depending on a CLASSPATH environment variable. Better to use the -classpath option for javac.exe when you compile and java.exe when you run.

javac -verbose -d build -classpath . CommandLine.java

java - Package does not exist error - Stack Overflow

java import package
Rectangle 27 0

You can use mod_rewrite to accomplish this. With mod_rewrite you can test if a requested file or directory exists, and if they do not exist you can do an external redirect:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILNEMAE} !-d
RewriteRule ^ http://64.233.185.94%{REQUEST_URI} [R,L]

Change the R flag to R=301 after testing that this works as expected.

whats the difference of R and R=301, does it important ? or its just for search engines ?

301 redirects are permanent as hjpotter linked. By using a permanent redirect clients can expect that redirect to never change, which means they can (permanently) cache the second url and load the second url on any subsequent request to the first url. For search engines this will prevent being penalized for duplicate content.

regex - Htaccess 404 - redirect - Stack Overflow

regex apache .htaccess mod-rewrite redirect
Rectangle 27 0

This problem can be caused by requests for certain files that don't exist. For example, requests for files in wp-content/uploads/ where the file does not exist.

If this is the situation you're seeing, you can solve the problem by changing this line:

RewriteRule ^(wp-(content|admin|includes).*) - [L]

The underlying issue is that the rule above triggers a rewrite to the exact same url with a slash in front and because there was a rewrite, the newly rewritten request goes back through the rules again and the same rule is triggered. By changing that line's "$1" to "-", no rewrite happens and so the rewriting process does not start over again with the same URL.

It's possible that there's a difference in how apache 2.2 and 2.4 handle this situation of only-difference-is-a-slash-in-front and that's why the default rules provided by WordPress aren't working perfectly.

Thank you Justin. Very informative. So do you think it's best to keep the rewrite rule in (with a - instead of $1) rather than commenting it out?

wordpress - Apache 2.4 - Request exceeded the limit of 10 internal red...

wordpress apache .htaccess rewrite
Rectangle 27 0

Redirecting with mod_rewrite to a 404 page if the directory and file do not exist.

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .? /404.html [L]

Here, -f matches an existing filename and -d matches an existing directory name. That will check to see that the requested filename is not an existing filename or directory name before it redirects to the 404 page (or whatever you like).

"virtual" Rails routes won't have files so this should fail. What am I missing?

ruby on rails - How do I configure Apache to handle missing image asse...

ruby-on-rails apache mod-rewrite