I have the following .htaccess:
RewriteEngine On
RewriteBase /sandbox/
RewriteRule ^(assets)($|/) - [L]
RewriteRule ^(.*)$ lib/script/bootstrap.php?route=$1.php [NC,L,QSA]
What I am doing is redirecting all requests (except for my assets - img, css, js) to my bootstrap script, which will handle the request via the route parameter, which includes the URL request.
This works fine in most cases, except for when I have a call such as:
http://www.example.com/sandbox/admin, where admin is a folder that exists in my directory, amongst other folders. This folder contains various pages that are accessible, aswell as an index.php file that my bootstrap.php routing will redirect requests from the above URL to it, by default.
My problem is that when I attempt to hit the page: http://www.example.com/sandbox/admin, my htaccess (as far as I know) is rewriting the visible URL to http://www.example.com/sandbox/admin/?route=admin.php, which is not as pretty. I believe it's because there's some conflict going on with the fact that the above URL is a valid URL pointing to my /admin/ folder, but I would like my .htaccess to ignore this and send this aswell to my bootstrap.php (line 4 of my .htaccess). What am I doing wrong?
Here is the requests coming in my browser:
UPDATE:
Strangely, http://www.example.com/sandbox/admin/ works fine, but not http://www.example.com/sandbox/admin (notice the trailing slash).
If you have real folders you need to use a condition to ignore real files and folders. All other requests will get routed to your bootstrap file. (If I understand your issue correctly.)
RewriteEngine On
RewriteBase /sandbox/
RewriteRule ^(assets)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ lib/script/bootstrap.php?route=$1.php [NC,L,QSA]
Related
I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...
I have a WordPress installation in my main folder, so my site goes like
www.example.com
Besides that I have a subfolder with a separate static site that you can access with
www.example.com/special
So in the special pages I have a page called
www.example.com/special/my-special-page
It's actually a .php file, but I've removed the extensions in the .htaccess file of that special site with this
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And this works.
But my client said: I want, when I enter
www.example.com/myawardspage
to go to
www.example.com/special/my-special-page
but to have the above URL (without /special/my-special-page) in it. So in the main .htaccess file (the one that controls the WordPress) I've added
RewriteRule ^myawardspage?$ http://www.example.com/special/my-special-page [NC,L]
So now when you go to
www.example.com/myawardspage
I am redirected to
www.example.com/special/my-special-page
which is great, but I need the URL to look like
www.example.com/myawardspage
So in the .htaccess file of the special page I've added
RewriteRule ^myawardspage/?$ /special/my-special-page [NC]
But the URL remains the same (http://www.example.com/special/my-special-page).
What am I doing wrong here?
Just below RewriteEngine on, place the following lines in your /.htaccess (document root) file:
# Check if the file being requested exists
# in the 'special' directory
RewriteCond %{DOCUMENT_ROOT}/special/$1.php -f
# If so, then rewrite accordingly:
RewriteRule ^([^.]+) special/$1.php [L]
Using this method means that you don't need that rule in the special/.htaccess file - you can safely remove it.
Ok, so you mean when ever you want to hit www.example.com/myawardspage then you want to get data from www.example.com/special/my-special-page while in your address bar your address remains www.example.com/myawardspage To achieve this apply this rule in your WordPress installation's .htaccess file.
RewriteRule ^myawardspage/?$ www.example.com/special/my-special-page [NC,L] # Handle requests for "www.example.com/special/my-special-page"
It wil get you desired data and your problem will be solved. Let me know if that works for you.
There are similar situations and answers here to redirect a url to a specific file using mod-rewrite. I tried them but none of them worked for me.
I would like to redirect all requests to a file and handle everything by that file. It worked fine somehow, but when there are virtual sub-folders, it does not work at all.
For example:
http://test.com/this_page_does_not_exist.php
works fine. but when I use something like this:
http://test.com/no_sub_folder_here/this_page_does_not_exist.php
it does not work fine. It tries to find css files for 404 page from no_sub_folder_here. So what I need to have is to address
http://test.com/no_sub_folder_here
to
http://test.com/
and then load the css for the 404 page. my css is in the root.
Here is my .htaccess file
RewriteEngine On
# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward it to 404.php in current directory
RewriteRule . %{ENV:BASE}/404.php [L]
it does not redirect or change the directory from sub-directories inside 404 to root. It renders the codes, but the page style is all wrong because of missed css from
test.com/style.css
it checks
test.com/no_sub_folder_here/style.css
instead.
Also, after a couple of tries, it does not change the url form the no_sub_folder to root. So, after a while, I will get a long list of no_sub_folder in my url :(
Because you change the base path, you have to fix that, with:
<base href="http://test.com/">
in html <header> of your 404.php page.
Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.
I have a proxy I've been writing with PHP for a while.
Right now, requesting a url like www.mysite.com/proxy/?folder/page.html on my server will return the page at www.theirsite.com/folder/page.html.
I basically am having my index.php file use everything past /proxy/? as the request URI for www.theirsite.com. Any images are copied to the folder /proxy/images/ and the src attributes of the <img> tags are changed accordingly. All this is working great.
Now I would like to change my script so that I will not need the ? anymore. However, the url www.mysite.com/proxy/folder/page.html would result in an HTTP request to page.html, which doesn't exist on my server.
This isn't what I want. I need index.php to be loaded instead, so it can return the page at www.theirsite.com/folder/page.html. To accomplish this, I imagine I would need to use Apache's mod_rewrite, which is working with my WordPress installation.
What would I need in my .htaccess file to do this correctly, while still allowing access to files that exist in the /proxy/images/ directory? Would this affect $_SERVER['REQUEST_URI'] at all?
RewriteEngine on
RewriteRule ^/$ /index.php [R]
Well, I got it working how I wanted. Here's the full contents of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /proxy/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy/index.php [L]
</IfModule>
Now for the url www.mysite.com/proxy/folder/page.html, index.php is loaded, and $_SERVER['REQUEST_URI'] returns /proxy/folder/page.html. Files that actually exist in the /proxy/ folder or any subfolders do not rewrite to index.php, which is what I wanted.