I wrote this code to redirect everything to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
I want to somehow adjust !-f to allow .php files to be 301 redirected to / without getting into a redirection loop. I have /foo/bar/218 that loads index.php which requires view/foo_bar.php. I want /view/foo_bar.php to be inaccessible via the address bar.
You can try these rules:
# redirect .php requests to /
RewriteCond %{THE_REQUEST} \.php[\s?] [NC]
RewriteRule ^ / [L,R=302]
# existing rules without .php files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.php$ index.php [QSA]
Related
I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]
When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service
So I have been working on some rewrite rules which aren't working as expected. These are some sample requests I am trying to rewrite:
/ -> /index.php?page=Home
/Home -> /index.php?page=Home
/Teaching/Foo -> /index.php?page=Teaching&id=Foo
/Teaching/Bar -> /index.php?page=Teaching&id=Bar
/Download/ue8 -> /index.php?action=Download&id=ue8
/Download/24a -> /index.php?action=Download&id=24a
(default) -> /index.php?page=Home
** OR ALTERNATIVELY **
(default) -> /index.php?page=FileNotFound
(and maybe rewrite the visible URL to /FileNotFound)
I mainly want to hide as much of the urls as possible and prevent both directory listing and direct access to my files located in specific folders and only allow access to downloadable files via /Download/FileId while having my usual pages for different lectures accesible via /Teaching/SomeLecture.
So far I have been using this snippet for the /Home and /Teaching stuff:
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Teaching/([A-Za-z0-9]*)$ index.php?page=Teaching&id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Home$ index.php?page=Home [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,R=301]
I am not completely sure about all of these directives and I noticed, that there are currently some flaws to it.
Trying to access a non-existant file, e.g. /Files/Bad/Path.pdf, forwards the user to /?page=Home which should be redirected either to /, /Home or display the contents of /index.php?page=FileNotFound without changing the URL at all or redirecting to /FileNotFound depending on the rule of (default). I am not really sure which solution might be the most suitable in this scenario.
Trying to access some folders which do exist results in an infinite redirection loop while folders which do not exist apparently redirect to /. In both cases it feels right to redirect to /FileNotFound I suppose?
Could you please work out a set of rules that might suit my needs in this case?
You have many redundant directives in your .htaccess. Replace all of your .htaccess with this:
# Turn off mod_spelling
<IfModule mod_speling.c>
CheckSpelling off
CheckCaseOnly off
</IfModule>
Options -MultiViews
RewriteEngine On
RewriteBase /
# block direct access to file and directories in these directories
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(Templates|Files) - [NC,F]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# Redirect Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(Download|Teaching)/([\w-]+)/?$ index.php?page=$1&id=$2 [L,QSA,NC]
RewriteRule ^(Home)?/?$ index.php?page=Home [L,NC,QSA]
Make sure to clear your browser cache before testing this.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# load index.php by default
DirectoryIndex index.php
RewriteBase /
# for all other requests load room.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(index|room)\.php).+)$ room.php?u=$1 [L,NC]
My htaccess currently will redirect anyone to room.php if the url contains anything else but the root directory such as http://www.example.com/anythingelse if not it will simple direct to index.php
But I need to be able to not include this redirect for particular patters so I can create content in particular folders. For instance
http://www.example.com/1
http://www.example.com/2
http://www.example.com/3
http://www.example.com/4
is this possible?
You can add these exclusions in your regex pattern:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# load index.php by default
DirectoryIndex index.php
RewriteBase /
# for all other requests load room.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(1|2|3|4)
RewriteRule ^((?!(index|room)\.php).+)$ room.php?u=$1 [L,NC]
I need to
Remove the file extension from all of the urls on my site (mywebsite.com/about.html >> mywebsite.com/about/)
Always add a trailing / to the end of the url (mywebsite.com/about >> mywebsite.com/about/)
Allow for an exception that one of the nav items links to a pdf, not an html document (mywebsite.com/calendar.pdf >> mywebsite.com/calendar/
Allow subdomains to go to their folder instead of being rewritten (My folder tree is public_html>my main site files and a dev folder>(inside dev folder) index.html) I need the subdomain to link to that dev folder, currently the url rewriting changes dev.mywebsite.com to public_html/dev.html
This is my current, hacked together htaccess file:
RewriteEngine On
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Sorry, this might be a duplicate or semi-duplicate. I looked around as much as I could but couldn't find anything that applied to my situation exactly or that I understood. Thanks guys!
Try this code:
RewriteEngine On
RewriteBase /
# skip dev. subdomain
RewriteCond %{HTTP_HOST} ^dev\. [NC]
RewriteRule ^ - [L]
# calendar rule
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf [L]
# hide .html rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]