I have the following .htaccess file on my server (It's a tweak of code I found on-line somewhere, for the life of me I cannot remember where) and I'm not quite sure how to amend it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
It basically grabs the URL, removes any trailing /, adds .php to the end of it, goes to that page, but then displays the link with a / instead of the extension in the URL bar (from what I can tell).
It works really well, bar two annoying niggles:
I can then only have .php files within that directory. Any others break.
The .htaccess file needs to be copied to EVERY subdirectory to function.
Can this script be improved upon at all, to do the following:
Put it in my root (/) directory and have it work in to all sub-folders?
Allow multiple file extensions within each directory? (e.g. some .php, some .html)
This code should work from your DocumentRoot and will add all the extensions:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.+)$ /$1/ [R=301,L]
Related
Here's my situation: I have a directory which I need to rewrite URLs in, so that ?page=hello can be accessed at either /hello or /hello/. I then have a sub-directory which needs .php extensions to be internally appended, so /subdir/example.php can be accessed at /subdir/example and /subdir/example/.
I developed the two parts of the site independently, and I'm now having trouble getting htaccess to meet both requirements.
My current .htaccess file is:
RewriteEngine On
# The root directory bit...
Redirect 301 /home /
RewriteEngine On
RewriteCond %{REQUEST_URI} !/subdir
RewriteRule ^([A-Za-z0-9-_/]+)/?$ index.php?page=$1 [L]
# Remove .php
RewriteEngine On
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
The rules work perfectly independently, ie if I remove the other one, but, together, does append.php but the rewrite rule fails.
I've tried making two separate htaccess files, one for the subdir and one for the root dir, with the appropriate rule (nice URL rule for root dir and remove .php for subdir), but this just means that appending .php no longer works.
Does anyone have any solutions to this?- or any resources to point me in the right direction? I'm currently in a hole of PHP and Apache documentation!
This is what worked:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(subdir/.*?)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
This does only remove .php for files of the subdirectory, but is certainly better than nothing.
I have created a sort of AWS S3 proxy PHP script, which hides a lot of files behind username/password.
My problem is that directory-like urls has to have trailing slash appended for links in the html files to work.
Ex: foo.com/link should rewrite to foo.com/link/
File urls should still be file urls.
Ex: foo.com/folder/image.png -> foo.com/folder/image.png
I have found lots of Apache mod_rewrite rules like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
But RewriteCond %{REQUEST_FILENAME} !-f depends on the actual requested folder to exist on the server. And since i do fetch the actual folders and files from AWS S3 it fails.
My current .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
RewriteCond $1 !^(redirect\.php|test\.php|css)
RewriteRule ^(.*)$ redirect.php?file=$1 [L]
Has anybody had this issue before and found a solution?
Extended info: I have implemented automatic add of index.html, index.htm, default.php ect. in the redirect.php-scriot for folder-like urls to work. But the static links in the HTML is broken if the trailing slash is not added.
In PHP MVC, How to show external css, js and image files?
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I could not show any external files.
The code above matches the ${REQUEST_URI}, e.g. matches at all uris, where you probably want to match the requested filename ${REQUEST_FILENAME}
Moreover, the code above also matches non existent symlinks ("!-l") and non existent directories ("!-d").
Probably only matching non-existent filenames would work perfectly:
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
Now, only requested filenames that don't physically exist on in the folder index.php resides in will be aliased to index.php.
For speed purposes, or if Apache rejects to display your static css and such you can disable the RewriteEngine on a given directory, for example:
<Directory "path/to/public_html">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
</Directory>
<Directory "path/to/public_html/static">
RewriteEngine Off
</Directory>
The order of the directories might be the other way around if the example above doesn't work. Also make sure to add this to your .htaccess file or in your <VirtualHost>-tag.
I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
today i wanted to achieve this thing where .htaccess automatically removes any sort of file extension from the url on the browser.
Lets say for example user clicks a link to take the user to
mylink.php
It should take him there but remove the .php
i tried using a few .htaccess code but they did not do much.
Also i can access the pages like so
localhost/register << this one works fine but i want the extension to be removed auto
localhost/register/ << this one looses all styling because it behaves like a page in new folder
The .htaccess code is
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any ideas people?
RewriteEngine On
# Change this to the appropriate base (probably just / in your case)
RewriteBase /stackoverflow/9762238/
# Any url that requests a .php file on the base will be redirected to dir with same base name
# The condition is important to prevent redirect loops!
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^(.*)\.php$ $1/ [R=301,L]
# Requests for directories that do not actually exist will be be translated into php file requests
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ $1 [L]