mod_rewrite remove subfolder and php

I want to make my url go from

root.com/sub/?page=home

TO

root.com/home

I want to remove the subfolder, remove the php and just leave the most basic url. Also if possible i want to redirect all non 'root.com/[*]' back to root.com. For example 'root.com/home/index.php?page=home' and 'root.com/sub/test/' would both redirect back to 'root.com'.

.htaccess and mod_rewrite seems to be the best way So far i have this:

RewriteEngine On #remove subfolder, WORKS RewriteCond %{HTTP_HOST} ^root\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.root\.com$ RewriteCond %{REQUEST_URI} !^/sub/ RewriteRule (.*) /sub/$1 [L]

This works and removed the subfolder

#remove PHP, not working :S RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1 [L] RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1 [L]

But this is not working and leaves the '?page=home' ect I think im missing something before the regex for the second rule, but im new to this.

Thanks in advance!

EDIT: taken into account the [L] thing, still no dice.

I'm guessing it needs to be done in one hit rather then seperate iterations.

-------------Problems Reply------------

The problem of mod_write usually lies on conflicts; in this case, the second block will be matched by the first block append afterword, creating an infinite loop or 500 error.

You should append the flag [L] to every RewriteRule. E.g.

RewriteEngine On
#remove subfolder
RewriteCond %{HTTP_HOST} ^root\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.root\.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule (.*) /sub/$1 [L]

#remove PHP
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1 [L]

Without [L], /abc will be rewrite to /index.php?page=abc, and that will rewrite to /sub//index.php?page=abc.

I am 75% sure this is the problem, but it's untested so I am not going to bet on it. Never hurt trying though.

This rule is the problem I think:

RewriteRule (.*) /sub/$1 [L]

Try changing it to:

RewriteRule (.*) /sub/index.php?page=$1 [L]

Category:regex Views:0 Time:2011-01-24

Related post

  • mod_rewrite: remove trailing slash (only one!) 2010-06-18

    I use mod_rewrite/.htaccess for pretty URLs. I'm using this condition/rule to eliminate trailing slashes (or rather: rewrite to the non-trailing-slash-URL, by a 301 redirect; I'm doing this to avoid duplicate content and because I like URLs with no t

  • How to remove subfolder from url 2010-08-16

    I moved my site to a subfolder called /corp/. So to get to the site you type in mywebsite.com/corp/index.php I would like to remove the /corp/ so it looks like mywebsite.com/index.php Thanks for all the help! --------------Solutions------------- You

  • agh - mod_rewrite remove '-xxx-XXX' from URL 2010-08-06

    bit of a nightmare. Clients sent out a big email with in links in them which have subsequently changed! just need to change forthcoming-events/event/skills-xxx-XXX to forthcoming-events/event/skills so just removing the '-xxx-XXX'. Any help majorly a

  • mod_rewrite: remove query string from URL? 2010-08-11

    I'm trying to make the following redirection (301) using .htaccess *?page=1 redirects to * (where * is a wildcard). Basically, I just want to prevent anyone accessing a page with ?page=1 at the end of the URL, and instead direct them to the same url

  • mod_rewrite Remove .php not working 2011-06-21

    RewriteEngine on RewriteCond %{http_HOST} ^domain.com [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond %{REQUEST_FILENAM

  • mod_rewrite remove a GET variable 2011-10-13

    I'm trying to take example.com/home?lang=fr&foo=bar and redirect to example.com/fr/home?foo=bar I tried RewriteCond %{QUERY_STRING} lang=([a-z]{2})&? RewriteRule ^(.*) /%1/$1 [R=307] but I get a redirect loop. This is because the lang=fr is n

  • Mod_rewrite remove appending URL 2011-11-03

    I am sure this might have been asked before but I've been pulling my hairs for two days with no luck, what I want to do is do a 301 redirect for www.domain1.com/mydesk/anything to www.domain2.com/blogs. So far what I've been able to do is get the /my

  • Mod_rewrite - remove question mark, not only query string 2011-11-24

    I need to redirect an url like mydomain.com/index.php to mydomain.com/ This sounds easy but the software framework also uses urls like mydomain.com/index.php? Those urls (with an single-added question mark) should not redirect to mydomain.com. The pr

  • mod_rewrite remove query string on top of a rewrite rule 2012-02-29

    I have the following rewrite rule: RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L] When I tried to open: /heru-task-number-1.html It is working fine. HOwever, when there is a query string appended to it: /heru-task-number-1.h

  • mod_rewrite: remove query string from end of url URL 2012-05-02

    I'm trying to make the following redirection (301) using .htaccess for mp3, jpg, jpeg, png & gif files only. http://website.com/file.jpg?c=XXXX is replaced with http://website.com/file.jpg Basically, I want to strip the query (ie. "?c=XXXX") from

  • .htaccess mod_rewrite remove .php extension, but preserve URL path 2011-06-07

    I currently have PHP scripts that work properly, being called like this: www.example.com/user.php/paul and www.example.com/tag.php/food I'm having trouble getting .htaccess to rewrite properly. I'm trying to achieve this: www.example.com/user/paul ww

  • CI: Controllers in subfolder - removing subfolder from url 2011-06-21

    I want to group my controllers, views and models into public/ and members/ subfolders. But for the public stuff, I don't want /public/ to show in the URL, so: http://mysite.com/ & http://mysite.com/section/ should point to: /public/home & pub

  • Running a CFWheels site from a subfolder instead of the web root 2011-03-24

    My CFWheels site is running under subfolder/ in the web root, and I have the web.config on the same level as subfolder/. In my web.config I have the necessary URL rewrite rule to remove "subfolder/index.cfm/" from the URL. My problem arises when I tr

  • Url rewritting with Apache mod_rewrite 2011-09-06

    In my PHP web page I want to redirect as follows: header("Location: page2.php?id=$tid"); The querystring is needed by page2.php, but I don't want the querystring displayed in the visitor's browser. I thought I could eliminate the querystring using mo

  • How to match a MAC address in mod_rewrite 2012-03-14

    Imagine a URL like this: http://hostname/a/b/xy-00:11:22:33:44:55-/c I would like to put an .htaccess file in the root and process it as: http://hostname/d.php, where d.php is really stored in the filesystem as /home/user/d.php I have the following i

  • htaccess: how to redirect every url that starts with http://site.com/shop 2010-10-02

    Like the title says, how do I redirect every url that starts with http://site.com/shop to a single page? so I have: http://site.com/shop/index http://site.com/shop/detail/52 http://site.com/shop/download .... etc, and it should be redirected to http:

  • Hiding offset in pagination 2012-04-05

    i have following url structure http://www.mydomain.com/vpn-offers-p3.html this page should be redirect to http://www.mydomain.com/vpn-offers.php?st=60&page=3 here st is offset for the pagination and will be varying with page, while page is the cu

  • Removing a folder from the URL using Apache mod_rewrite 2009-09-24

    How do you remove a folder from the URL? I have a Drupal installation in a folder and I would like to remove that subfolder from the URL. So instead of: www.example.com/subfolder/* I would like the url to be: www.example.com/* thanks --------------So

  • mod_rewrite to remove .php but still serve the .php file? 2009-11-09

    I just wanted to do a simple thing with mod_rewrite. I have a site which uses .php files, and I wanted to rewrite those to cleaner URLs, and remove the .php. So, files would be www.mysite.com/contact and so on. This does work how I wanted, but I had

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.114 (s). 11 q(s)