I am using the following .htaccess file, which works well, however, I want to also forward any attempts to access non-existent files in the root directory and in any subdirectory (whether the subdirectory exists or not) back to the main domain name.   I tried FallbackResource but that resulted in 500 series errors.  RewriteEngine On  RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^https://xxxxxxxxx.xxxxx/.*$ [NC] RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|png|css|pdf)$ -     [F]  # browser requests PHP RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]  # check to see if the request is for a PHP file: RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^/?(.*)$ /$1.php [L]  ErrorDocument 500 /redirect ErrorDocument 500 /redirect ErrorDocument 500 /redirect ErrorDocument 404 /error ErrorDocument 401 /redirect ErrorDocument 403 /redirect   Any help would be greatly appreciated.
487
This is a  about Apache's mod_rewrite.   Changing a request URL or redirecting users to a different URL than the one they originally requested is done using mod_rewrite. This includes such things as:   Changing HTTP to HTTPS (or the other way around) Changing a request to a page which no longer exist to a new replacement. Modifying a URL format (such as ?id=3433 to /id/3433 ) Presenting a different page based on the browser, based on the referrer, based on anything possible under the moon and sun. Anything you want to mess around with URL   Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask!  How can I become an expert at writing mod_rewrite rules?   What is the fundamental format and structure of mod_rewrite rules? What form/flavor of regular expressions do I need to have a solid grasp of? What are the most common mistakes/pitfalls when writing rewrite rules? What is a good method for testing and verifying mod_rewrite rules? Are there SEO or performance implications of mod_rewrite rules I should be aware of? Are there common situations where mod_rewrite might seem like the right tool for the job but isn't? What are some common examples?   A place to test your rules  The  web site is a great place to play around with your rules and test them. It even shows the debug output so you can see what matched and what did not.
48829
This is a  about redirecting from http to https in Apache Related:     I have an Appache web server which serves both http://example.com/ and https://example.com/. I want to redirect all http requests to https. Currently I'm using this .htaccess rule to redirect http requests to https. RewriteEngine On RewriteBase / RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}   It's working as expected for example.com but the same rule is not working for sub links, when I access existing links, like example.com/about it will still load in http, no redirection happens for existing links. How can I make Apache redirect all http requests to https?
93190
I am reviving/reorganising my personal WordPress blog.  It's using a URL that looks like this:  http://mydomain.com/blog  The webserver 301 redirects www.mydomain.com to mydomain.com.  I want to use the blog subdirectory because I plan to add other parts to the site, with the blog only being one part of the site.  However, at the moment there is nothing there but the blog, so I want to have the root index page redirect to the blog for the time being.  I have been using this on the root index.html page to do the redirect...  &lt;meta http-equiv="REFRESH" content="0;url=./blog"&gt;  ...but this seemed to have stopped the site being indexed by Google and Bing.  How do I do this without affecting SEO?  Also, what URL should I put in the sitemap.xml?
43257
You can make a catch-all rewrite rule for apache that acts somewhat like an alias, but can be implemented in a .htaccess file while an alias can't.  As an example, assuming your document root is a hierarchy like this:  /.htaccess /test/ /test/.htaccess /test/file   You can "Alias" all requests to the test folder by making the root .htaccess contain:  RewriteEngine On RewriteRule ^(.*)$ /test/$1 [L]   And the second .htaccess contain:  RewriteEngine On   This works great, and usually is all you need. It just has one problem - it creates multiple valid urls. Now you can access the file with both /file and /test/file.  Ordinarily this isn't a problem, but if you add a file called /test/test you'll see the problem, as the fake alias stops working entirely. If the subfolder has a complex .htaccess of its own it can cause major headaches.  Is there any way to fake an alias from a .htaccess without the original urls still working? (Any RewriteCond for the subfolder that would check if we've already been rewritten or not?)
66255
I am having a tough time removing the subfolders from the URL in my WordPress site.  Currently, the URL format is: www.mysite.com/subfolder/index.php/category/postname  I would like to remove both the subfolder AND index.php from the URL.  The site must stay in a subfolder of root on my server.   My current htaccess file is:  &lt;IfModule mod_rewrite.c&gt; RewriteEngine On RewriteBase /subfolder/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /subfolder/index.php [L] &lt;/IfModule&gt;   I've tried to add the following, although it did not work.  Options +FollowSymlinks RewriteEngine on RewriteRule ^subfolder/(.*)$ http://www.mysite.com/$1 [R=301,L] RewriteRule .* index.php/$0 [PT,L]
116624
I'm configuring nginx's handling of some error pages and other "default" media files (such as favicon.ico and robots.txt) at the moment and I ran into a minor problem getting things to work the way I want for certain error pages.  Basically, what I'm trying to do is serve certain files for a server under the root for that server, e.g. /var/www/someserver.com/robots.txt. If that file does not exist, I want nginx to go to the "default", i.e. /var/www/default/robots.txt. This is the basic gist of how I have that (successfully) configured:  server {     ...     root /var/www/someserver.com;      location ~* ^/(robots\.txt)$ {         error_page 404 = @default;     }      location @default {         root /var/www/default;     } }   That works great.  I'm trying to do the same for error pages, and I'm not able to make that happen though:  server {     ...     root /var/www/someserver.com;      error_page 404   /404.html;      location ~* ^/(404\.html)$ {         error_page 404 = @default;     }      location @default {         root /var/www/default;     } }   Note that this "works" in the sense that if you visit someserver.com/404.html, it will first try to load /var/www/someserver.com/404.html and then fall back to /var/www/default/404.html if that's not found. However, if you visit someserver.com/blahblah, it only shows the 404 page if it's set in /var/www/someserver.com/. It does not fall back to the default directory if that file doesn't exist.  Anyhow, you can probably what I was trying to accomplish (that's why I included the first working example).  Any ideas?  Edit:  Based on Martin F's answer, this is what I ended up putting together:  # Doesn't work when error page is returned on a POST request server {     ...     root /var/www/someserver.com;      error_page  404         = @notfound;     error_page  500 502 504 = @server_error;     error_page  503         = @maintenance;      location @notfound {         try_files /404.html /../default/404.html =404;     }      location @server_error {         try_files /500.html /../default/500.html =500;     }      location @maintenance {         try_files /503.html /../default/503.html =503;     } }   This works awesome. The actual block of error_pages and locations above is in a server_defaults.conf file that gets included with every virtual host, which is why I didn't hard code the path into each location and used a relative path for the defaults.  Edit 2:  This approach has a problem. If you POST to a URL that returns an error, the POST request method gets sent with the try_files attempts. This (for me) results in a 405 Not Allowed error, because nginx is essentially trying to POST to e.g. /default/500.html instead of just getting that page.  Edit 3:  I posted a solution that works that's a lot closer to my original idea.
35370
I have a PHP application (Wordpress) in the root folder of my site, and all requests to the site run through it happily.   However, I have a subfolder, _links, which I would like to use to execute another, different php script.  Eg, if a user goes to site.com/_links/221312, then the index.php script from /_links takes over.  I formerly used a .htaccess file to achieve this, but since moving to nginx I can't make it work.   Here is the old .htaccess file.  RewriteEngine On RewriteRule    ^([0-9]+)/?$    index.php?linkid=$1    [NC,L]   Obviously, I'm passing the last bit of the URL to the script as a parameter.  Here is my nginx config. Requests to eg site.com/_links/23423 are picked up by if (!-e $request_filename) and rewritten to /index.php?q=/_links/23423 instead of being picked up by the location block.   server {         listen  127.0.0.1:8080;         server_name site.com www.site.com m.site.com;          root /var/www/site;         location ^~ /_links/ {                 add_header location 1;                 root /var/www/site/_links;                 rewrite "^([0-9]+)/?$" /index.php?linkid=$1 break;                 location ~ \.php$ {                         try_files $uri =404;                         fastcgi_pass unix:/var/run/php5-fpm.sock;                         fastcgi_index index.php;                         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                         include fastcgi_params;                  }          }         index index.php index.html;         if (!-e $request_filename)             {                 rewrite ^(.+)$ /index.php?q=$1 last;             }         location / {                 try_files $uri $uri/ =404;         }          # pass the PHP scripts to FastCGI server listening on the php-fpm socket         location ~ \.php$ {                 try_files $uri =404;                 fastcgi_pass unix:/var/run/php5-fpm.sock;                 fastcgi_index index.php;                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                 include fastcgi_params;          }         access_log /var/log/nginx/site.com.access.log;         error_log /var/log/nginx/site.com.error.log notice; }   I'm really confused. What's going on here?
41769
In Google Search Console/Webmaster Tools, I found 404 error URLs with this pattern: http://example.com/page-name/example.com  This is happening to all pages, posts, and even category type.  For example:   Here is the .htaccess file:    Does anyone know what might be causing this?
10371
I'd like to do some URL rewriting (Why? See .) so that instead of users seeing addresses like   labouseur.com/course-compilers.html    they can instead see and use simply   labouseur.com/course-compilers   (Even better, maybe I should restructure that so that it's courses/compilers.)  I'm using a Linux-based shared hosting service for my website, so I do not have administrative control of the server, but I do have control over .htaccess. The references I've read online seemed less than clear to me, so I'm looking for a little clarity and advice here.
78217
I have created a subdomain and a CNAME record which points to the domain root. The subdomain www.static.example.com is actually a copy of the entire website www.example.com and it is supposed to act as an CDN and serve static content in order to improve speed. However, all of my content can be accessed via subdomain aswell, so Google has indexed it all and now I am dealing with duplicated content.  How could I deny access to crawlers for the subdomain baring in mind that I do not have different subfolder for subdomain, so I can't create a separate robots.txt file?
56911
I've been working with Apache http servers for quite some time, and finally making the move to static-content servers alongside the others dynamic-content machines.   I was wondering, does nginx support ".htaccess" files, and things like mod_rewrite?   As I'm very used to the syntax, I was wondering what the (syntax) differences were, and what the learning curve is like moving from Apache configs to nginx.
121104
In Nginx we have been trying to redirect a URL as follows:  http://example.com/some/path -&gt; http://192.168.1.24   where the user still sees the original URL in their browser. Once the user is redirected, say they click on the link to /section/index.html, we would want this to make a request that leads to the redirect  http://example.com/some/path/section/index.html -&gt; http://192.168.1.24/section/index.html   and again still preserve the original URL.  Our attempts have involved various solutions using proxies and rewrite rules, and below shows the configuration that has brought us closest to a solution (note that this is the web server configuration for the example.com web server). However, there are still two problems with this:   It does not perform the rewrite properly, in that the request URL received by the web server http://192.168.1.24 includes /some/path and therefore fails to serve the required page. When you hover on a link once a page has been served, /some/path is missing from the URL  server {     listen          80;     server_name     www.example.com;      location /some/path/ {         proxy_pass http://192.168.1.24;         proxy_redirect http://www.example.com/some/path http://192.168.1.24;         proxy_set_header Host $host;     }      location / {         index index.html;         root  /var/www/example.com/htdocs;     } }    We are looking for a solution that only involves changing the web server configuration on example.com. We are able to change the config on 192.168.1.24 (also Nginx), however we want to try and avoid this because we will need to repeat this setup for hundreds of different servers whose access is proxied through example.com.
78463
Does anyone know if there is an equivalent to .htaccess and .htpassword for IIS ? I am being asked to migrate an app to IIS that uses .htaccess to control access to sets of files in various URLs based on the contents of .htaccess files.   I did a google search and didn't turn up anything conclusive. Is there a general approach or tool that I need to purchase that provides this capability ?
107373
Due to a  in nginx I was  to switch my alias directive with a root directive. Unfortunately this now breaks my static files, which are located in a different directory to my site path (mysite.com/blog -> /var/www/mysite/wordpress). How can I get around this, without changing my static file structure? Here's my full location block:  location /blog {     root /var/www/mysite/wordpress;     try_files $uri $uri/ /blog/index.php$is_args$args;      location ~ \.php {         fastcgi_pass 127.0.0.1:9000;         include fastcgi_params;         fastcgi_split_path_info ^(?:\/blog\/)(.+\.php)(.*);         fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;     } }
69138
The situation  Across the entire domain, we'd like the URLs to hide file extensions and remove trailing slashes, independent of the domain name itself (as in, works on any domain).  Sample of our directory structure  We're not using index.* files except for the homepage.   /  /index.php /account.php /account  /subscriptions.php  /login.php /login  /reset-password.php     The goal  Some examples of how these files might be requested, and how they should look in the browser:   / and index.php --> mydomain.com (literally just the bare domain name). /account.php or /account/ or /account --> mydomain.com/account /account/subscriptions.php or /account/subscriptions/ or /account/subscriptions --> mydomain.com/account/subscriptions   As you can see, there are several ways to access each webpage, but no matter which of the 2 or 3 ways you use to get there, it only shows the one preferred URL in the browser.  The question  How is this done with .htaccess using mod_rewrite?  I've banged my head against the wall trying to figure this out, but in general, the rewrite flow would seem to be something like this:   External 301 redirect ( mydomain.com/account/ --> mydomain.com/account ) Internally append .php ( mydomain.com/account --> mydomain.com/account.php )   I've been Googling this all day, read thousands of lines of documentation and config texts, and have tried several dozen times... I think more brains on this would help a lot.  UPDATE  We found an answer our question (see below).
35684
I have this in my .htaccess (modified from the default Drupal .htaccess):  RewriteCond %{HTTP_HOST} ^www\.website.com\.com$ [NC] RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]   In theory this will redirect something like http://www.website.com/asdf to http://website.com/asdf. However, it doesn't redirect, it allows requests to go through like there was no redirect intended.  (I'm on OS X if it matters, but I've also tried it on a Network Solutions Linux server with the same result.)
91310
I'm currently working on a plugin, and I essentially want to check if a certain page has been created.  Essentially, I want to do something like this:      if (file_exists($file)) {     $exists = true; } else {     $exists = false; }   But I just want to do it so that it checks, instead, for a post_name value. (If I was further able to ensure that the post was a page and not just a post with that title, that would be great too.)  How do I do this?  I had been getting the script to grab the headers from the url of the post and if it returned a 404, to return $exists = false, but I've pinpointed a memory leak problem with that and I don't think it's very efficient.