tags:

views:

65

answers:

3

I re-did my portfolio site recently, and I'm trying to make it so that when someone clicks a link to my and old project (ie /philosophy.php) that it takes the name of that file (philosophy) and then adds it to an id indicator # and loads to my new page with that ID selected so it automatically scrolls to that project.

Click link > http://www.studioimbrue.com/philosophy.php Redirect > http://www.studioimbrue.com/#philosophy

I am using this right now: <meta http-equiv="Refresh" content="1; URL=http://www.studioimbrue.com/"&gt; and I added #<!--#echo var="$_SERVER['PHP_SELF']" --> but it just comes up as (none).

A: 

Add a .htaccess file to your server root

and include this in it:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.php$ /#$1 [R=301,NE]

Otherwise use this:

<meta http-equiv="Refresh" content="1; URL=http://www.studioimbrue.com/#&lt;?php echo basename($_SERVER['PHP_SELF'],'.php');?>">
Pentium10
This will just add philosophy.php to everything, it needs to be a variable pulled from the original link (so if it were .com/book.php it would redirect to .com/#book)
steve
This redirects endlessly (at least on my server). You need some RewriteCond. You also need the NE (no escape) flag to correctly handle the hash.
jasonbar
Done...........
Pentium10
This works!I tried using the PHP one but the URL it sent ended up being this: http://www.studioimbrue.com/#<?php echo basename($_SERVER['PHP_SELF'],'.php');?>
steve
It sounds like that is not a HTML file.
Pentium10
A: 

If you want you can achieve the same thing with PHP

$newName  = 'http://www.studioimbrue.com/#' . substr($_SERVER["PHP_SELF"], 1, -3);
header('Location: $newName');
die();

But I think doing this with .htaccess is the better way.

Sinan
+1  A: 

You might want to add some rewrite condition not allowing the redirection when there actually is a file with that name, as well as specifying the rewrite rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.php$ /#$1 [R=301,NC,NE]
voodoo555
This will do what you want BUT you must add the NE flag otherwise your hash mark will be escaped and the browser won't read it.fRewriteRule ^([^/]+)\.php$ /#$1 [R=301,NC,NE]
jasonbar