I can’t seem to make this work - and I’m pretty sure that the real problem is that I just starring myself blind on it, so I hope a pair of fresh eyes can help me out.
What I wan’t to do is have several applications attached to my system.
At this time, a website already exists in the root folder, but I wan’t some microsites/powerformats in a CI installation.
My mod_rewrite looks like this:
RewriteCond %{REQUEST_URI} ^/(powerformat1|powerformat2)/?$
RewriteRule ^(.*)$ powerformats/index.php/$1 [L]
Although, getting the CI index.php correctly, when trying to access example.org/powerformat1 or example.org/powerformat2 gives me CI’s 404 page.
It seems like whatever I try of rewrite rules I either get the 404 page or nothing at all.
Any insights?
-- EDIT --
What I believe is my problem is that CI actually gets the 'powerformat1' string passed as the first segment. That is what I need to avoid. But can't that be solved through mod_rewrite?
You could try link directly to the file with the appropriate query string instead
RewriteRule ^(.*)$ /powerformats/index.php?somequery=$1 [L]
(you may have to change the slashes, see below)
Or it may be this:
Accessing /powerformat1/
may be rewriting to
powerformats/index.php//powerformat1/
You could try
RewriteRule ^/(.*)/$ /powerformats/index.php/$1 [L]
or some other variation with slashes:
RewriteRule ^/(.*)/$ /powerformats/index.php/$1/ [L]
Did you miss RewriteEngine On ?
Related
I have been battling with this problem for a while. Suppose I go to website.com/one/tWo. In PHP $_SERVER['REQUEST_URI'] is one/tWo. I thought this had something to do with Apache URL rewriting so I checked .htaccess and removed everything and only left:
RewriteEngine On
RewriteRule ^one/two http://www.gosomewhereelse.com [R=301,L]
To my surprise, when trying 'one/tWo', Apache actually redirects me to www.gosomewhereelse.com.
Just to be clear, there is no redirection that happens from 'one/tWo' to 'one/two'. The URL doesn't change in the address bar and it still has the uppercase character but it's still matching with ^one/two
I tried everything I could think of and couldn't find a solution.
This works:
RewriteCond %{REQUEST_URI} ^/one/tWo$
RewriteRule ^(.*)$ http://www.gosomewhereelse.com [R=301,L]
This also works:
RewriteRule ^one/t[W]o http://www.gosomewhereelse.com [R=301,L]
It turns out that it's not related to apache or PHP. The problem is from a third party software.
TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.
I have a single page website that changes content based on variables passed through the URL with PHP.
For instance, my url displays as
www.mysite.com/index.php?section=home
www.mysite.com/index.php?section=about-us
so forth and so on, depending upon which link you click in the main navigation.
I want the urls to read as www.mysite.com/home or www.mysite.com/about-us.
My mod-rewrite feature is enabled because before I made this a one page site, it was functioning correctly.
I've tried this...
RewriteEngine on
RewriteBase /
RewriteRule ^home/?$ index.php?section=$1 [NC,L]
I've tried every suggestion I found on Google and on StackOverflow, nothing is working.
Any help would be appreciated!
I have an .htaccess for exactly the same purpose, and it's working beautifully. My code is below.
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]
Naturally, if index.php isn't your main PHP file, replace that with what is. Also, replace ?id with ?section if your example code is what your using.
What the [^/\.]+ means is saying I need you to find text that contains anything but a period (.) or a forward slash (/), and there has to be at least one character in it.
For an example, go to the website I'm using it on, Northside Aikido.
If you have any questions, please feel free to ask.
EDIT
Note, this code won't turn http://www.example.com/index.php?section=home into http://www.example.com/home automagically, it'll just mean that the latter link works like the former. If you want it to automatically replace it, you'll need more code than one line.
this is what im using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?section=$1 [NC,L]
</IfModule>
Almost
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?section=$1 [NC,L]
I am trying to perform this type of rewrite
http://sitename/foo/var1/var2 -> http://sitename/foo/index.php?/var1/var2
This is my .htaccess file(placed in the directory foo):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
In my php script I am displaying the values of $_SERVER['REDIRECT_QUERY_STRING'] and $_SERVER['REQUEST_URI']
If I request a URL like http://sitename/foo/bar the values as expected are bar and /foo/bar respectively.
This also works as expected for: http://sitename/foo/admin and http://localhost/foo/bar.
However when I try to access http://localhost/foo/admin (using localhost instead of sitename) the REQUEST_URI changes to /admin/?admin (this is how it displays in the address bar too, i.e http://localhost/foo/admin/?admin)
I searched for any .htaccess files that might be conflicting and also turned on mod_rewrite logging at level 6 but was unable to find any info.
I have no clue what might be causing it. It would be great if I could know what might be causing this, otherwise I might switch to nginx.
Thanks all for replies. This was some weird server configuration error, things seem to be fine on the new VM I installed. #Dan Grossman , thanks for your suggestion about using $_GET, some of my code is simpler than earlier.
With the new Diggbar, you can put http://digg.com in front of any URL that you are currently at and it will create a Digg short URL. I am only assuming they do this by modrewrite (though I am not sure since I am new at this all).
How is this done? It seems to me when I try this with a website I am working on, it bombs out.
I want to be able to do the following:
http://example.com/http://stackoverflow.com/question/ask
and have a modrewrite that will allow this to go to
http://example.com/index.php?url=http://stackoverflow.com/question/ask
But when I use this modrewrite:
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ /message.php?id=$1 [L]
it doesn't work. What am I doing wrong?
You have to take the value from the request line because Apache removes empty path segments. The initially requested URI path /http://foobar/ becomes /http:/foobar/. But the request line (THE_REQUEST) stays untouched:
RewriteCond %{THE_REQUEST} ^GET\ /(https?://[^\s]+)
RewriteRule ^https?:/ index.php?url=%1 [L]
You're only looking for letters and numbers in that regular expression, so it won't pick up the colon and slashes. You're also using index.php in the example and message.php in the htaccess ;)
You'll probably want something like this:
RewriteEngine on
RewriteRule ^http://(.+)$ /index.php?url=$1 [L]
This makes sure you only catch URLs here, and you can still have regular pages! (Think about what would have happened if you went to example.com/index.php, you'd end up in an infinite loop.)