.htaccess url rewrite for single page website - php

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]

Related

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

Using $_POST data after wordpress url rewrite

My permalink structure is set so I have url.com/page
I made a basic PHP script prior to installing wordpress that uses $_POST data to display the correct set of information, so the base would look like url.com/work.php?featured=print
After adding this to my Wordpress installation with the rewrite from the permalink structure above, the link actually works as:
url.com/work/?featured=print
I'm having trouble getting the extra rewrite to work so that a clean url.com/work/print will work properly.
This is what my .htaccess file looks like, I appended the last line before the end IfModule tag hoping that would take anything work/[page]/ and direct it to work/?featured=[page]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^work/([^/.]+)/?$ work/?featured=$1 [L]
</IfModule>
# END WordPress
any help?
EDIT
Discovered rewrite via the functions page and found this quite resourceful
Rewrite rules for WordPress
however, my rule is taking .com/work/print (work/?feat=print) and simply showing it as .com/work without the page data being passed through
'work/([^/]+)/?$' => 'work&feat=$matches[1]'
The above is the only thing I changed from post I just referenced. I tried keeping is specific to get it to work first to avoid any loose ends...
Still not working properly though
The wp-admin dashboard has a tool that helps with the rewrites. When I ran a few WP sites, I used it to achieve what I wanted. Did you try that first?
http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/
For some reason, couldn't get anything to work, but ^ is quite simple and worked great

Pretty URL’s and mod_rewrite

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 ?

URL rewrite to remove parameters

I'm working on a site where all the pages are actually index.php + a 'name' parameter that is analyzed
and loads the appropriate template and content.
the homepage url is:
http://www.some_site.com/?page=homepage
1. i was asked to "change" the homepage url to:
http://www.some_site.com
can i use url rewite and htaccess for that and if so, what should i write there?
working on my local machine, i tried this code (mode rewrite is enabled):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule /index.php /index.php?page=homepage
</IfModule>
i would still need the 'name' parameter to be available to the php code of course, so i can load the
template and css files.
2. it would be nice for other pages (not homepage) to be converted from (example)
http://www.some_site.com/?page=products
to:
http://www.some_site.com/products
this is less crucial.
thanx in advance and have a nice day :-)
You don't need a rewrite rule at all. Just change your index.php file to show the homepage when there is no page variable at all.
if (!isset($_GET['page'])) {
$_GET['page'] = 'homepage';
}
For educational purposes, the rewrite rule:
RewriteRule /$ index.php?page=homepage [L]
That is, the URI to match is just the slash (the URI starts after your domain in the URL). The $ means that there should be no characters after the slash.
As for products and such, assuming single words made of only letters:
RewriteRule /([a-zA-Z]+)$ index.php?page=$1 [L]
The following should be what you're looking for (for your second, less crucial, question). Put it in your .htaccess-file:
RewriteEngine On
RewriteRule ^/([a-zA-z0-9-_]+)/?$ index.php?page=$1

Create SEO-friendly URL with PHP with just the Querystring Value

Is there a way use mod_rewrite to produce the result below?
Original URL:
http://www.domain.com/shop.php?id=newyork
to
SEO friendly URL
http://www.domain.com/newyork
I've seen plenty of example where the above URL can be converted to http://www.domain.com/shop/newyork but I actually don't want to display the word 'shop/' so just http://www.domain.com/newyork
I'd have a go with something like the following, off the top of my head
RewriteRule ^([a-zA-Z]*)$ www.example.com/shop.php?id=$1
Do bear in mind that anything after your root domain, will be piped into your shop.php script.
Yes, in your .htaccess file put
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/([^/.]+)$ shop.php?id=$1 [L]
</IfModule>
([^/.]+) will match anything that isn't a / or . and store that info,
$1 at the end outputs that info into your shop script.
[L] tells mod_rewrite to stop looking for rules if this one works.
Based on your example:
RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]
Would match newyork, alaska, hamburg but not highway-1.

Categories