Apache: How do I re-route all requests to a single file? - apache

I'm writing a static single page Backbone site with a single entry point: index.html
I've been told that because of this, I need to re-route all requests to my url e.g. www.example.com/*, to that index.html file. So, if someone types in www.example.com/lolnotreal, I need that request to be redirected to /index.html but in a manner which my index.html file still could pick up the url attempted, in the example's case: /lolnotreal
Is this even possible considering it's just an html file? Basically I need for backbone to pick up the url attempted.
Thanks for any help
Dearest downvoter: Please explain. I'm not a server expert and my hours of attempts at using .htaccess have failed.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
that does it.

Try to put this lines to your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9\/\_]{1,})$
RewriteRule [^/]*/(([a-zA-Z0-9\/\_]{1,}){0,})$ /index.html?q1=$0 [L]

Related

Folder is not visible in localhost

I have downloaded a web template and when i try to run that in my localhost the folder doesn't show up.that folder contains a .htaccess file.When i remove that file i can see the folder but when i open that it gives some errors.
This is the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ index.php [L]
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
this is the first time i'm working with a .htaccess file...
Any help is appreciated.
Maybe problem in the index.php file PHP code? Rewrite rules looks fine.
Show us the Apache error log and a rewrite log, without that hard to say what is wrong.
It looks like the RewriteCond directive is in the wrong place...? I would have thought it should apply to the 2nd RewriteRule, not the first:
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)?$ user-profile.php?user_username=$1 [NC,L]
RewriteCond directives apply to the single RewriteRule that follows. That 2nd ruleset basically says that if the requested file does not exist then internally rewrite the request to the user-profile.php page passing the request (which is assumed to be a username) to the user_username parameter.

URL Rewrite of Non-Existent File Under Subdomain Folder

My website structure is as follows:
.htaccess
index.html
internal
.htaccess
test.html
api.php
I have setup a subdomain in GoDaddy such that internal.ledworld-me.com maps to ledworld-me.com/internal and the following works fine:
Requesting internal.ledworld-me.com/test.html serves the test.html file correctly.
Now I need the following:
If internal.ledworld-me.com/nonexistent/file is requested, then the URL should be rewritten as ledworld-me.com/internal/api.php?__route__=/nonexistent/file.
What do I put in my .htaccess file in order to achieve this? And which .htaccess file should I put the code in, the one under WebRoot or the one under internal?
Please note that GoDaddy is taking care of the rewriting from internal.ledworld-me.com to ledworld-me.com/internal. All I need my .htaccess file to do is the second part of the rewrite, namely, /nonexistent/file should be rewritten as api.php?__route__=/nonexistent/file.
It's really hard to explain this question and I've tried my best, please ask for clarification if needed. Thank you!
Put this code in your /internal/.htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ api.php?__route__=/$1 [L,QSA]
The following text in my internal/.htaccess file seems to do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://ledworld-me.com/internal/api.php?__route__=/$1
I'm still testing all the scenarios and will confirm that everything is working once I'm done all the tests.

URL Rewriting and Redirecting with folders

I know there are lots of question on htaccess, yet I tried the different code I could find on Google and StackOverFlow, none worked.
I have the following in my root :
index.php
.htaccess (the one I am trying to write)
controllers
--index.php
--mycontroller.php
models
--mymodel.php
view
-index.php
--myview.php
(I am working on localhost with MAMP&Firefox)
What I have is this link
localhost:8888/MySite/controllers/mycontroller.php
What I want is
localhost:8888/MySite/mycontroller
And when I manually enter the url, I would like it to be redirected to the right controller in my MVC code
I tried this :
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)$ /controllers/$1 [L]
It doesn't redirect when I go to blabla/controllers/mycontroller.php and doesn't understand what I am asking when I manual go to blabla/mycontroller.
If your base is in /MySite/ then it needs to reflect the RewriteBase:
RewriteEngine On
RewriteBase /MySite/
# match against the php filename
RewriteCond %{REQUEST_URI} ^/Mysite/(.*)$
# check to see if the request, routed through controllers actually points to an existing file
RewriteCond %{DOCUMENT_ROOT}/MySite/controllers/%1.php -f
# rewrite
RewriteRule ^(.*)$ controllers/$1.php [L]
This should take a request for the URI: /MySite/foo and rewrite it to /MySite/controllers/foo.php if there's a foo.php file in the controllers directory.
Did this to make it work :
RewriteEngine On
RewriteBase /MySite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controllers/$0.php [L]

htaccess help, removing a string from the URL

I am wondering, is it possible to remove index.php from an URL? Basically on some pages in a site I have this structure,
http://www.domain.com/index.php/members/register, but other pages I have URL structures like this, http://www.domain.com/category/products/id/5, I want to know is it possible with htaccess to remove the index.php and any attributed slashes when needed? How would I go about doing this?
Yes, you can. With this rule any requested /index.php will be removed:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]
But you should better use the proper URLs right from the start so that you application is serving documents whose links don’t contain the /index.php.
If you want to globally rewrite index.php/controller/action
This .htaccess configuration should do the trick:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This configuration checks on Apache whether the file/directory exists on disk or not (i.e. the request match a real resource on disk), and rewrite the request to your front controller if needed.
So http://www.domain.com/resources/image.png should return the image resource.
And http://www.domain.com/user/show/5 should transparently rewrite to http://www.domain.com/index.php/user/show/5
With this configuration, you can remove all index.php references in your application URLs and leave the rewriting to the web server.

Remove ".html" from URL via .htaccess for a WordPress website

Background information:
I've searched stackoverflow for a specific solution and couldn't find one that fixed my situation. Thanks in advance for any help you can offer. Your knowledge is appreciated.
I've decided to accept a contract to "convert" (in the client's words) a Joomla site into a WordPress site. Everything is going along smoothly, except that the Joomla site links to .html files, both in its navigation and in the content of 100+ posts.
Instead of going through each post one-by-one and updating the links or running a SQL command to remove ".html" from URLs, I've decided to put the pressure on .htaccess, with which I am somewhat comfortable.
What I'm trying to do ↓
In WordPress, I have custom permalinks enabled, and it is as follows:
/%category%/%postname%
Here's an example of what one of the old URLs in the posts looks like:
http://the-site.com/category/the-webpage.html
I need the htaccess file to tell the webserver to remove the .html so the user, after visiting "http://the-site.com/the-webpage.html" is instead sent to:
http://the-site.com/category/the-webpage
I'm setting up the page stubs to follow the file name of the Joomla pages, so http://the-site.com/category/the-webpage will work.
My question:
Can you help me discover the solution to removing .html from the URL when someone visits the site, even if the HTML file doesn't exist on the server?
Here's how the .htaccess file looked before I made changes:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Here's the latest .htaccess file as of 5:35pm Eastern:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
The ↑latest .htaccess changes work. Thanks Tim!
This will work to force an external redirection to your new URLs, but this may not be ideal for your situation. I'm still trying to think if there's a way to keep the redirection internal and update the variable that WordPress uses to determine which page to serve up, but so far I haven't thought of anything that would work.
Entire .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You want to use a URL rewrite
RewriteEngine On
RewriteRule ^(.*)\.html$ $1
This should do it. It will rewrite a request to site.com/category/whatever.html to site.com/category/whatever. it shouldn't be dependent upon the requested file existing.
<Directory /var/www/category>
RewriteEngine on
RewriteRule (.*)\.html$ /category/$1
</Directory>
This is the format for apache2.conf or virtual host files. Not sure if you use the command in .htaccess. It's best to take care of it in the server conf, if you can, as that is only parsed once, on server startup, and htaccess is parsed on each request.