2 Replies - 8633 Views - Last Post: 04 March 2012 - 07:08 AM

#1 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

.htaccess URL Rewrite: from "/?page=calendar" to "/calenda

Posted 03 March 2012 - 11:39 AM

I simply just want to clean up my URLs a bit; see example in title. I want to convert the URL from a page query to just a forward slash and then the variable. I've been to countless sites trying to figure this out but I haven't gotten it to work the way I want it to. The only thing that I've gotten to "work" is this, but it works backwards from the way I want it to:

RewriteEngine on
RewriteBase / 
RewriteRule ^page/(.*) ?page=$1 [R]


Which results with:

Quote

Input: localhost/page/calendar
Output: localhost/?page=calendar


I do have the following code that semi-does what I need it to do:

RewriteEngine on
RewriteBase / 
RewriteCond %{QUERY_STRING} page=(.*)$
RewriteRule (.*) /%1?  [NC,L,R=301]


Which results with:

Quote

Input: localhost/?page=calendar
Output: localhost/calendar


Which is what I want, but instead of going to "index.php" in the root folder, I assuming it's handling the "/calendar" as a sub-directory rather than a variable, so I get a nasty 404 error.

If I place "index.php" in the rewrite rule before the /%1? like this:

RewriteRule (.*) index.php/%1?  [NC,L,R=301]


It gives this:

Quote

localhost/index.php/calendar


Which works slightly, except for styles and images don't work again other than on the main page, but I am able to get what I need from the URL in regards to variables by using this code:

$url = explode("/", $_SERVER['REQUEST_URI']);
echo $url[2]; // used to be $url[1] but since the "index.php" is there I had to change it to "2"


The above code will print 'calendar' and in itself it "functions" properly, but has the ill effects on my styles and images.

I've been at this for days and it's driving me up the wall. Can anyone please help me out? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: .htaccess URL Rewrite: from "/?page=calendar" to "/calenda

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: .htaccess URL Rewrite: from "/?page=calendar" to "/calenda

Posted 04 March 2012 - 05:59 AM

View Postgoku87, on 03 March 2012 - 06:39 PM, said:

Which is what I want, but instead of going to "index.php" in the root folder, I assuming it's handling the "/calendar" as a sub-directory rather than a variable, so I get a nasty 404 error.

If there is no other rule that says that /pagename URLs should be rewritten to ?page=pagename, then the HTTP server will just process the URLs the usual way.

What you need to do is have two rules: one that internally rewrites /pagename URLs to ?page=pagename, in order to allow users to request /pagename but it still being processed by the index file; and another rule to create an actual redirect for ?page=pagename to /pagename, so users trying to use the former will still end up with the latter.

The first one is pretty simple. Your first snippet is actually one variation of it, only using /page/pagename instead of just /pagename. This variation does that:
RewriteRule ^([^/]+)/? index.php?page=$1 [L,QSA]



For the second, you need to use a RewriteCond with your RewriteRule. Your second example is actually pretty close to this as well, but I would suggest something like the following instead. It does one thing your version did not, which is keep any additional query parameters as well, while still redirecting to the /pagename URL.
RewriteCond %{QUERY_STRING} (.*?)page=(\w+)(.*)
RewriteRule ^ /%2?%1%3 [L,R=301]



If you put those two together, you end up with a system that does these types of rewrites:
Entered :  /?page=pagename
Redirect:  /pagename
Internal:  index.php?page=pagename

Entered :  /?page=pagename&some=value
Redirect:  /pagename?&some=value
Internal:  index.php?page=pagename&some=value

Entered :  /pagename
Internal:  index.php?page=pagename


From there you just access the pagename from the list of GET values, like $_GET["pagename"] in PHP.

View Postgoku87, on 03 March 2012 - 06:39 PM, said:

Which works slightly, except for styles and images don't work again other than on the main page, but I am able to get what I need from the URL in regards to variables by using this code:

You can fix that by adding another RewriteCond to your RewriteRules. This will exclude most common resource files from being processed like normal pages:
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|png|gif|ttf|eot|swf)$ [NC]


Just add whichever extensions you need.


By the way, I moved this thread to the Web Servers & Hosting forum. This isn't exactly a PHP related question.

This post has been edited by Atli: 04 March 2012 - 06:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: .htaccess URL Rewrite: from "/?page=calendar" to "/calenda

Posted 04 March 2012 - 07:08 AM

Wow, thank you so much! It works great! Although, I'm not quite sure how.

Is there a website that explains how the rewrite engine works in plain english (or might you be so kind as to explain it)? Maybe I was just looking for the wrong things, but I couldn't find any explanation on how it works, as in proper syntax and whatnot.

For instance:

RewriteRule ^([^/]+)/? index.php?page=$1 [L,QSA]


  • ^([^/]+)/? - yeah, I don't even know what this is
  • index.php?page=$1 - this is the only part I understand
  • [L,QSA] - I didn't know what to search for to figure out what these last letters are


Educate my lack of understanding, please.

This post has been edited by goku87: 04 March 2012 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1