htaccess remove file ext for folder - php

I've tried looking at other tutorials and examples but nothing quite fits my scenario. My htaccess file looks like this:
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^view/([^/]+)/([^/]+)/?$ index.php?page=view&id=$1&title=$2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)
RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L]
ErrorDocument 404 /404
I'm using PHP and need to rewrite /index.php?page=page&moreparamaters=1
But I also have a folder on the server called /admin/ and I want to remove the .php extension from the files in this folder so that:
/admin/lol.php becomes /admin/lol/
I have lots of these files so I can't really add each one in, individually. How can I do this without affecting my index.php rewrites?
Thanks

you have to think the other way round. Assuming that all your files are placed in the folder directly (meaning, no subfolders), this will work:
RewriteRule ^admin/([^\/]*)? /admin/$1.php [L,R]
In your links you may then write /admin/lol/ or /admin/lol, both will be redirected to /admin/lol.php

Related

.htaccess started redirecting images

So I've been messing around my website (which was working yesterday) and by mistake I've rewritten my .htaccess file with blank one (wanted to duplicate the file in one folder into different folder, but messed it up making it rewrite root folder's .htaccess), so I'm trying to make one again (yes I know, should have had backup).
Now to the problem: I was editing unrelated thing in my .htaccess file, and suddenly the images stopped showing, being redirected to my 404 error page instead. It looks to me like the Apache suddenly started ignoring my certain RewriteCond rules, as css and javascript rules normally work, and it's driving me insane.
I've tried to search everywhere, but haven't found the solution.
This is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !=/index_new.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index_new.php?sekce=$1 [NC,L]
RewriteRule ^([^/]+)/$ index_new.php?sekce=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3&podpodpodsekce=$4 [NC,L]
The question was marked as DUPLICATE, but I've tried the solutions (before writing the question) and they do not work, as I use absolute paths to images too, but I cannot get to them regardless.
Managed to get the backup from the webserver provider. If anyone would have same kind of problem, the working code is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule \.(css|jpe?g|gif|png|gif|js|ico)$ - [L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^([^/\.]+)/?$ /index_new.php?sekce=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3&podpodpodsekce=$4 [L]

How to hide directory name from URL using .htaccess

Here is my site url structure as follows: http://www.sitename.com/new/about-us .
What I really want to do now is hide the directory name 'new' from the above url, but the admin url should remain unchanged.
The admin url would be like : http://www.sitename.com/new/admin .
My previous .htaccess code as follows:
RewriteEngine On
RewriteBase /new/
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/new/$1 [R=301,L]
RewriteRule blog/ - [L]
RewriteRule (^wlp) - [L]
RewriteRule admin/ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule ^([\S\s/+.]+)/?$ index.php?url=$1 [QSA,L]
Here is my server directory structure:
/public
/new
.htaccess
index.php
about-us.php
/blog
/admin
Any help in this regard will be highly appreciated.
It sounds like what you are trying to accomplish is a common technique which I've seen before for forwarding all public_html requests to public_html/public to essentially hide the contents of the public_html directory from the user and making public_html/public the new web root.
Try using this in your public_html/.htaccess file (you can write further htaccess in public_html/new/.htaccess):
RewriteEngine on
RewriteBase /
#enforce www subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#forward all requests, except new/admin, to the 'new' directory without the user's awareness
RewriteRule new/admin - [S=2]
RewriteRule ^$ new/ [L]
RewriteRule (.*) new/$1 [L]
This .htaccess in / should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^new/
RewriteRule ^(.*)$ new/$1 [L]

Allow access to a specific php file in CI root

I created a test.php in my root. and wanted it to be accessible but its giving me a 404 error. Please see below for my .htaccess config.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I tried adding "RewriteCond $1 !^(test.php)" above the rewriteRule but its still not accessible. Can anyone give me an advice on how I can make the test.php accessible in a browser, I want to access it in a browser by going to this URL: www.domain.com/test.php
Thanks in advance!
Try this:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|css|test\.php)
RewriteRule ^(.*)$ ./index.php/$1 [L]
Every request will be sent to your index.php except those mentioned in RewriteCond, i.e., css, images and test.php
Replace your first two lines with the following.
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Also your index.php should be accesible with or without that code so it's not needed.

URL Rewriting domain to use subdirectory as root; still shows that directory in URL in Analytics?

I set up a certain domain to rewrite into a folder, shown below, but have been having people show up in that folder in Google Analytics.
in short: /c/ -> root when from domain, but GA still shows domain/c/ in Visitors Flow
root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt
Options -Indexes
and /c/'s .htaccess is just an edited html5boilerplate one with the only difference being the same php and txt rewrite above.
But on Google Analytics, more than half of my visitors are still ending up on /c/index.php.
Visitor Flow: http://i.imgur.com/JwtcTFI.png
I haven't seen /c/ in my URL, so I really don't have a clue what's going on.
Any ideas? I do use relative URLs (is this a bad idea?) so that causes the flow to continue in the /c/ folder.
Thanks for the help!
Your rules do nothing to prevent people from directly accessing /c/whatever. If you want to prevent that, you need to add an extra rule:
RewriteCond %{THE_REQUEST} \ /c/
RewriteRule ^c/(.*)$ /$1 [L,R=301]
Oo, I found the answer! It seems like www.domain was redirecting to domain/c/index.php. I had a rewrite rule to remove the "www" in the /c/.htaccess, but not the root .htaccess.
I fixed it by adding code to remove the "www" in the root htaccess too BEFORE the domain>>directory rewrite. (after didn't seem to work)
I added
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
above
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
(in the root htaccess)
edit: It also probably has to do with the (www.), I dropped that due to redundancy. Now it's just RewriteCond %{HTTP_HOST} ^certain-domain.com$ [NC]
I also added a rewrite for /c/ to root in the /c/ .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /c/
RewriteRule ^(.*)$ /$1 [L,R=301]

Simple htaccess rewrite/redirect to search page

I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.
Here is what I have.
RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]
What am I doing wrong?!
Here is the complete file
ErrorDocument 404 /index.php?p=404
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]
RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
Make the first line perform a Redirect and the second perform a Rewrite
RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]
and move
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]
to the end of the set of rules.
Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.
I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]
RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]

Categories