Clean/Short URLs using .htaccess is not working as it should - html

I've been following some YouTube videos to try and get this working but it doesn't seem to want to. This is how my .htaccess file is looking:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule RewriteRule ^(.*)$ index.php/$l
I am completely new to .htaccess, I'm not completely sure how it works but my common sense tells me to just create a file, call it .htaccess and place it in the same folder as my index.php, right?
So when I go to, say index.php/foo/bar or even just /foo/bar/ I get a 404 error.
What are the possible problems? It's a Windows 2008 Server with PHP 5.3.10 installed.

As beginning your last line should be
RewriteRule ^(.*)$ index.php/$l
And it should work as you expect it... pass all requests but ones for existing files/folders into index.php
The .htaccess can be in any folder which is hit by the request URL... the only difference is the base of the URL which is relative to the folder where is the .htaccess located
Example:
/folder1
.htaccess
index.php
/folder2
.htaccess
index.php
If you access /test.php none if your .htaccess files will be processed (it's not related to the request URI.
If you access /folder1/test.php the corresponding .htaccess in folder1 will be used and the $1 in your RewriteRule will show test.php, the /folder1/ will be stripped from the URI.
If you access /folder1/folder2/test.php it's similar... apache will use .htaccess from folder2... the one from folder1 will be ignored (a bit counderintuitive)

Related

url rewriting to hide a directory

My website directory contains 2 folders : website1 and website2
So to access them:
http://www.example.com/website1
http://www.example.com/website2
I want to access them with these URLs:
website1: http://www.example.com
website2: http://www.example.com/website2
so I write this .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ website1/$1 [L]
This works well for website1 but if I want to access website2 it doesn't work (because it redirects to http://www.example.com/website1/website2 I think)
How I can write this file to redirect only when URL is http://www.example.com?
You can either:
Create another .htaccess in the /website2 and disable the rewrite engine:
RewriteEngine Off
This will prevent the mod_rewrite directives in the parent .htaccess from running, so the request is not rewritten.
Or,
Modify the existing mod_rewrite directives in the .htaccess file in the document root to exclude requests to the /website2 subdirectory. For example:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/website2
RewriteRule (.*) website1/$1 [L]
The ! prefix on the CondPattern negates the regex, so it's only successful when the REQUEST_URI does not start with /website2.
You could also combine this into a single directive, which is marginally more efficient:
RewriteRule !^website2 website1%{REQUEST_URI} [L]
Note that the REQUEST_URI server variable already contains the slash prefix, so this must not be duplicated in the RewriteRule substitution.
I think you don't need htaccess for this. Put your website1 files in main root. Create a new folder and name it website2. Put website2 files in this new folder. I think this is what you want.

Errors with htaccess

Today I tried to write a .htaccess file for first time in order to remove .html extension from url bar along with some other things that I wanted to do. Since I was unfamiliar with all this I read several articles before coding.
I ended up with the following code. I also removed .html from all links. The problem is that when visiting my domain I get the following error.
//The resource you are looking for has been removed,
//had its name changed, or is temporarily unavailable.
Is my code correct?
UPDATED .htaccess
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
//Remove .html UNTIL NOW THIS IS THE ONLY PART
//OF THE CODE THAT ACTUALLY WORKS.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
//Caching schema
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "private, max-age=160704000"
</FilesMatch>
//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
In a separate .htaccess
//Prevent directory listings
Options All -Indexes
MAIN ERRORS
If I click on link like:
Home
Then I get the same error.
Thank you all in advance.
This is native in Apache without using mod_rewrite by using MultiViews option.
Documentation states:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
I finally managed to resolve a part of the problem. For starters I changed my server from windows to linux.
Then to remove html extension I used this code in .htaccess (the other codes didn't work well, giving me an error message)
//Remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I also removed any .html instances from all links.
Now it works like a charm.

How to htaccess 301 redirect pages with a question mark in the url

I'm trying to redirect several pages that all have question marks in the URL.
I essentially want to redirect:
www.example.com/?attachment_id=456 to www.example.com
There's a ton of pages with differend id #s also.
I've tried a few things in htaccess with no luck..
Any ideas?
This is what I tried:
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]
RewriteRule ^/$ http://www.example.com/? [L,NC,R=301]
Why can't you do this? This code should redirect a URL like this www.example.com/?attachment_id=456
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]+
RewriteRule ^/?$ http://www.example.com/? [L,NC,R=301]
I made the / optional so that it can be used in Apache config or .htaccess. Also I kept the ? that you have in the redirect at the end of the RewriteRule to remove any query strings on redirect.
Your approach is next to perfect, just some minor corrections:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^/$ http://www.example.com/ [L,R=301]
The above is the version for the host configuration. note that you have to restart the http server after having made changes to the host configuration for them to get effective. To debug refer to the http servers error log file, especially at restart time.
If you have to rely on .htaccess style files, then the syntax for the rule itself must unfortunately be slightly different:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^$ http://www.example.com/ [L,R=301]
Such file has to be located in the main folder of the document root of the host. also the interpretation of such files must be enabled in the host configuration by means of the AllowOverride option.
In general you should always prefer the host configuration for such rules over .htaccess style files, but you need administrative access for that. .htaccess style files are notoriously error prone, hard to debug and really slow the server down.

My domain www.digitalproductshopping.com is on a WordPress installation, Can I add a sub-directory that is html5

Can I have a HTML5 Sub Directory as part of my WordPress installation. I have put the subdirectory in public.html folder, but when i enter the URL www.digitalproductshopping.com/affiliatemoneysecret, I get a page not found 404 error on this URL.
Step 1 : create folder into your website directory ...parallel with folders : "wp-content" , "wp-admin" and "wp-includes"
Step 2 access your html page by www.yoursitename.com/folder where you can add html file and also run
The issue is basically due to .htacess rewrite rule
1)Try adding
ErrorDocument 401 default
to your htacess file , in most of the cases this should work if not then try adding
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/subdirectoryname1/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/subdirectoryname2/(.*)$ [OR]
RewriteRule ^.*$ - [L]
the sub directory was different, not in spelling but in capitalisation, in different documents, subdirectories must be spelled and capitalised exactly the same or the above error occurs.
Thanks for the effort everyone.

How to have a pure HTML (from index.html) directory in a cakePhP site?

I have a cakePhP site all set up. It has its "webroot" directory and some sub-directories that each have their own "app" directory. They all work fine. Now I want to set up a sub-directory that runs from just an "index.html" file, no "app" directory or cakePhP directory structure with a Controller file etc. It would be a pure HTML sub-directory.
If I just add a subdirectory containing just an "index.html" file (no ".htaccess" file in it) I get a cakePhP error saying the controller file for that directory is missing.
The ".htaccess" file of the overall "webroot" directory looks like this. I cannot change that ".htaccess" file because the other sub-directories depend on those rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Does anyone know what I need to do e.g. set up a local .htaccess file in my sub-directory to make my sub-directory work with just HTML?
Inside webroot/html folder create some html files, index.html, ...
test http://somesite.com/html/index.html
A possible solution is to hard code your sub-directory as the the first RewriteRule. Let's say your regular HTML sub directory is called "html".
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule /html$ app/webroot/html [L]
RewriteRule /html/(.*) app/webroot/html/$1 [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This way it will check if you are trying to access the sub directory before the rules for Cake are checked. I haven't tested this, but give it a try.