I have a real weird issue
I'm playing around with .htaccess and trying to redirect all requests to the /test/ folder's index file.
My site lies in a folder /test/ in my local htdocs folder. No other files exist currenlty.
What I expect:
When I visit any url, (for example /test/category/one/) I should be redirected to /test/index.php
What happens
I get a 404 Not Found
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]
I have tried setting RewriteBase /test/
This is as straight forward as it gets so why isn't it working?
I have a Wordpress site in another folder and that works flawlessly with custom rewrites.
I even copied the Wordpress' .htaccess contents to the test site's, substituting the rewrite base and last rule with /test/.
Wordpress' .htaccess: (which works on a seperate WP install on same server)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
# END WordPress
I have been struggling with this for a while now and read quite a few SO articles with no help.
I even write a rewrite log file and now there shows nothing when I browse to the test site but a visit to the Wordpress site writes quite a few lines.
I am running XAMPP on a Win64 machine.
Any help would be greatly appreciated! =)
Update: Also, make sure the line endings in your .htaccess file are set appropriately. Apache can sometimes choke on anything that doesn't include the new-line (\n) character.
So it looks to me like you want to (at some level) emulate what WordPress is doing. Here's how I handled this case when I was developing some software that did the same thing:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>
For files that exist (i.e. either the -f or -d test passes), we serve them up unchanged. Otherwise, we redirect incoming requests to index.php. Note that the /test portion of the path is not included in the RewriteRule, since the RewriteBase set up where we were starting from. So, in your example, I think it would end up being:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>
FWIW, I'm no .htaccess expert. I've simply found this to work for me in the past.
Also, if you're on a shared host (like DreamHost), you may need to set up the appropriate rules for allowing default error documents. Some shared web hosts serve up a single file (failed_auth.html is one example) for error cases. If you're not filtering out that case, you may end up with a 404.
This should do the trick:
# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]
Related
Background
I'm creating a time tracking app with PHP on my localhost (MAMP). The app structure is as follows
htdocs/time-tracker/public/index.php
Issue
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
What I need
I want the following result. When I visit the url 127.0.0.1:8888/time-tracker on my local machine, I trigger the php app, routing all requests through the htdocs/time-tracker/public/index.php. Preferably without a trailing slash, but priority is just to get the app to work.
My current .htaccess file
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
Updates
1. $_GET parameters change outcome
For some reason http://127.0.0.1:8888/time-tracker?debug=true and http://127.0.0.1:8888/time-tracker get me different results.
http://127.0.0.1:8888/time-tracker?debug=true results in a redirect to http://127.0.0.1:8888/public
http://127.0.0.1:8888/time-tracker results in a redirect to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
Neither of these results are what I want.
2. Partially working
This .htaccess file has gotten my redirects to work whenever I put something in the $_GET
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L]
For example, 127.0.0.1:8888/time-tracker/?test=test works while 127.0.0.1:8888/time-tracker/ still redirects to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
3. Not redirecting properly on root
The redirects works on all paths except for the root path. For example, 127.0.0.1:8888/time-tracker/test and 127.0.0.1:8888/time-tracker/?test=test both work, just not 127.0.0.1:8888/time-tracker/
I don't know why my regex won't pick this up. Here is my code
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php [L]
4. Seeing an empty path name
I've tracked it down to one last issue: empty paths don't register with the redirect.
# Settings
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine On
# Rules
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*|x) index.php?/var=$1 [L]
For some reason, it just can't catch the redirect if the path is empty.
5. Close enough solution
This is the best I got. This is actually working, but I couldn't fix the trailing slash issue.
# Settings
DirectorySlash On
RewriteEngine On
# Rewrite
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?/var=$1 [L]
Hopefully somebody can come solve the trailing slash directory root issue or at least confirm that it is impossible. At this point, the correct answer goes to anyone who can explain my mistakes and make this into a helpful post.
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Try this. Put your .htaccess file in the time-tracker folder
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I have a VPS Server (Debian 9) and I want to have clean url.
If I enter for example "examle.com/example", this pops up an error "Internal Server Error" instead of showing the page.
What do I need to do to make this mistake disappear and show the page?
That style of URL (domain.tld/resource/path) would need to be handled via mod_rewrite (with Apache) or a try_files configuration with Nginx. Which you use depends on which HTTP server you're running, of course. Unless you're explicitly setup Nginx, it's probably Apache.
Assuming Apache (httpd) is being used- The easiest method to manage this is via a .htaccess file in your document root (where your index.php file lives). You'll need to verify mod_rewrite is enabled in your httpd configuration (usually found at /etc/httpd/httpd.conf or broken out further in a subdirectory).
There are a lot of configuration options available to you via mod_rewrite. Here's an example of one that will translate all URLs that do not point to an actual directory or file (thus preserving your ability to serve static content directly from httpd) to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You would need to parse the URL to determine what to do with it (it will be contained within $_SERVER['REQUEST_URI']. Be sure to reference the mod_rewrite documentation if you need to make changes to this configuration.
try this I hope its help you
create a .htaccess file in your html folder and write blow code in it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
</IfModule>
or try below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
and the last one is for trailing slash at the end. (only use codes as your needs only).
<IfModule mod_rewrite.c>
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]
</IfModule>
more you can understand on
thanks.
I don't know why but all the time it shows me the error "500", even if I have empty .htaccess and basic files apache2.conf and 000-default.conf
I'm trying do use mod_rewrite at my .htaccess but isn't working.
my url is http://gestor.samfbas.com.br/index.php?p=something
it should be http://gestor.samfbas.com.br/something
The file is in a subdirectory (gestor) in my host, where all the files are.
<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>
Try this one (if index.php is in the root folder http://gestor.samfbas.com.br/index.php):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
This should work. Testet it here on my local maschine (No other server redirects or else, just a fresh xampp installation).
It redirect http://gestor.samfbas.com.br/something to http://gestor.samfbas.com.br/index.php?p=something without changing the url in the browser.
And additional to the question in the comment.
This URL part p= should not be known be outside users!
Better use a long var here like sadff34dngn4nil212ugn=, so nobody can call the index.php with parameters directly from outside. You can't prevent that 100% but the redirect parameter p= is only for internal use.
But its just my opinion on that.
Hopefully that helps a little.
Find the right way to rome ;)
I'm trying to route all requests to /web/index.php (front controller) while serving static files directly if they exist.
The following .htaccess file was taken from a Symfony 2 application, and seems to work fine as it is. Requests are sent to ./index.php and I can access to static files like ./web/css/style.css. I'm working on a shared hosting (not a good one) and this .htaccess seems the only one working (this will cause a 500 error).
How can I have route all requests to /web folder? It seems that I need to change something in E=BASE variable, I've tried [E=BASE:%1/web] but it doesn't work.
I have a little understanding of rewrite rules, can you point me to the right direction?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
I may be way off here... but isn't it just: change the last line to:
RewriteRule .? %{ENV:BASE}/web/index.php [L]
For the static files it needs to be looking in your /web folder to check if the file exists. Currently the RewriteCond %{REQUEST_FILENAME} -f just checks if the filepath requested exists.
Try throwing a RewriteBase command in before the RewriteCond %{REQUEST_FILENAME} -f:
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} -f
You can replace your .htaccess with this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^((?!web/).*)$ web/$1 [L,NC]
Let's assume that your domain is gremo_silex.com, and your hosting has directory for it like
~/domains/gremo_silex.com/public_html
(all hosting services I used last few years had directory structure similar to this).
Then you can place your Silex project one directory up (in ~/domains/gremo_silex.com) and rename your web directory to public_html, this way there's no web in your url's.
I encountered the same issue today. Normally the web host would allow you to change Apache's document root (my preferred method) but in this case it wasn't possible. So with some hackery I compiled this:
<IfModule mod_rewrite.c>
RewriteEngine on
# Serve all public files from /web/
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
# If file doesn't exist send to front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>
The REQUEST_URI header comes in to PHP without the /web prefix too, so routing should work out of the box.
I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here