Vanity URL, slight problem

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Vanity URL, slight problem

Post by EcazS »

I didn't find an appropriate section so I posted it here...

Anyways, this is my .htaccess file,
[syntax=text]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC]
[/syntax]

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?
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Vanity URL, slight problem

Post by Kamal »

[syntax=text]RewriteRule ^/articles/([0-9]+)$ index.php?articles=$1[/syntax]
if it doesn't work, try removing the slash after ^
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post by EcazS »

How would I do the links then?
[syntax=xhtml]<a href="articles/$id">Blabla</a>[/syntax]
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Vanity URL, slight problem

Post by Kamal »

EcazS wrote:How would I do the links then?
[syntax=xhtml]<a href="articles/$id">Blabla</a>[/syntax]

Add a / after href=" so the link won't be broken when you click on from an article.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post by EcazS »

Doesn't seem to be working, I have this,
[syntax=text]
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
[/syntax]

and on my index page I have,
[syntax=php]
if(isset($_GET['articles'])) {
echo "Working";
}else {
echo "Not working";
}[/syntax]
and then I go to localhost/projects/cms/articles/1 but I get "Not Working"
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Vanity URL, slight problem

Post by jacek »

[syntax=text]RewriteRule ^(.*)$ http://localhost/PhpStormProjects/CMS/index.php?page=$1 [NC][/syntax]
this rule, will match anything. If you swap them around so the articles one comes first, that might work.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Vanity URL, slight problem

Post by jacek »

Try this

[syntax=text] 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][/syntax]
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 ;)
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post 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,
[syntax=text]
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]
[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Vanity URL, slight problem

Post by jacek »

hmm...

what does that rule mean though :?

ah well, as long as it works :D
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Vanity URL, slight problem

Post by jacek »

What does linking to /home do ? You could also try ../home but that would break the folders above.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post by EcazS »

/home does the same as ./home :S
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Vanity URL, slight problem

Post by bowersbros »

EcazS wrote:/home does the same as ./home :S


./home is different to ../home
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post by EcazS »

bowersbros wrote:./home is different to ../home


I know, what's your point?
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Vanity URL, slight problem

Post by bowersbros »

Well..

/home is exactly the same as ./home

try ../home
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Vanity URL, slight problem

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Vanity URL, slight problem

Post 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.
Image
Post Reply