Page 1 of 1

Vanity URL, slight problem

Posted: Thu Jun 09, 2011 4:57 pm
by EcazS
I didn't find an appropriate section so I posted it here...

Anyways, this is my .htaccess file,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC]
and this is working great for the index page when you're on a page, index.php?page=Home turns into just Home. Fine, right?
But what if I wanted to add another RewriteRule to the index page, say, index.php?articles=$1.

I tried adding a new rule just like that but then everything turns into articles. Or can you somehow "fake" a folder.
I'm using a template system so all the content goes on the index page even blog post/articles.

And currently I'm just linking each article to index.php?article=id but I'd love for it to be like http://localhost/PhpStormProjects/CMS/Articles/id without having a real articles folder, if you get what I mean.
Is that even possible?

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 5:07 pm
by Kamal
RewriteRule ^/articles/([0-9]+)$ index.php?articles=$1
if it doesn't work, try removing the slash after ^

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 5:23 pm
by EcazS
How would I do the links then?
<a href="articles/$id">Blabla</a>

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 5:26 pm
by Kamal
EcazS wrote:How would I do the links then?
<a href="articles/$id">Blabla</a>
Add a / after href=" so the link won't be broken when you click on from an article.

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 5:50 pm
by EcazS
Doesn't seem to be working, I have this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC]
RewriteRule ^/articles/([0-9]+)$ articles.php?articles=$1
and on my index page I have,
if(isset($_GET['articles'])) {
     echo "Working";
}else {
     echo "Not working";
}
and then I go to localhost/projects/cms/articles/1 but I get "Not Working"

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 6:10 pm
by jacek
RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC]
this rule, will match anything. If you swap them around so the articles one comes first, that might work.

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 6:12 pm
by EcazS
I still get "Not Working", am I even going to the right URL- or should I change the other rule to NOT match everything, will that little PHP "script" even work properly for this? :S

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 6:26 pm
by jacek
Try this
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^/articles/([0-9]+)$ articles.php?articles=$1 [L]
    RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC]
The [L] means no more rules will be processed after that one matches. I removed one rule as it looked like it should not be doing anything useful, you can obviously put it back ;)

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 6:33 pm
by EcazS
Now I get "500 Internal Server Error", even if I go to the index page, localhost/projects/cms/index.php :shock:

Edit,
I got it to work by re-adding that other rule,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^articles/([0-9]+)$ articles.php?articles=$1 [L]
RewriteRule ^(.*)$ index.php?page=$1 [NC]

Re: Vanity URL, slight problem

Posted: Thu Jun 09, 2011 9:17 pm
by jacek
hmm...

what does that rule mean though :?

ah well, as long as it works :D

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 8:41 pm
by EcazS
This problem is related to this so I hope it's OK that I don't start a new topic.

I'm using that .htaccess file I posted as working and a "navigation template" that grabs URLs from the DB or rather names from the DB and then echoes the name in an anchor tag.

Problem is, if I go onto articles/2 and then click Home it'll take me to articles/Home and there is no article with the ID home so I get a pleasant visit from Mr. 404.

And on my links if I do ./name it takes me to the root, which is localhost. And I have all the files in localhost/Projects/CMS. Only for testing now then, on my live site it's going to be in the root but I wanna think ahead if I where to use it inside of a folder :)

Anything on this?

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 9:17 pm
by jacek
What does linking to /home do ? You could also try ../home but that would break the folders above.

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 9:24 pm
by EcazS
/home does the same as ./home :S

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 9:40 pm
by bowersbros
EcazS wrote:/home does the same as ./home :S
./home is different to ../home

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 9:47 pm
by EcazS
bowersbros wrote: ./home is different to ../home
I know, what's your point?

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 10:20 pm
by bowersbros
Well..

/home is exactly the same as ./home

try ../home

Re: Vanity URL, slight problem

Posted: Sun Jun 12, 2011 10:28 pm
by EcazS
bowersbros wrote: try ../home
If I do that and I'm not in a "subfolder" I will go down to localhost/projects when I actually want to stay in localhost/projects/cms

Re: Vanity URL, slight problem

Posted: Mon Jun 13, 2011 12:58 pm
by jacek
This is probably why phpbb has you enter the folder that the system is in in the admin panel. there may be no way around it.