views:

303

answers:

2

Hi,

Actually I want to run ~name/how instead of ~name/how.php.

I have made following changes in .htaccess:

# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_fileNAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_fileNAME} !-f
# then add .html to get the actual filename
RewriteRule (.*) $1.php [L]

But it will run as:

~name/~name/how

And at every link click it will be added ~name in the URL ex. http://ip/~name/~name/~name/serach.

Can you tell me what is wrong in .htaccess?

+1  A: 

You are using relative, not absolute paths. Your pages now think you are in a path ~name/. You can set <base href> in your pages.

You could possibly simplify this by using 'DirectoryIndex index.php' to map /whatever to /whatever/index.php

This may help: http://stackoverflow.com/questions/144651/how-to-use-apaches-modrewrite-rewriterule-without-changing-relative-paths

EDIT: Short answer. You should consider using absolute paths for your menus and php includes to prevent issues when you start nesting deeper. Your relative paths are only valid while you are two levels deep (ie, ~laborfa2/search/)

Frankly you links are mess so you're getting confused. I strongly recommend creating a very simple test site to get you paths organised properly. Use absolute paths and relative paths as appropriate. Remove the base href as it's just confusing you more. Once you have it running try out rewrite again keeping in mind your relative paths may need to be updated if the path depth is changed by the rewrite..

SpliFF
Actually I am new in mod rewrite /.htaccess.So can you please elaborate what is actually need to change??
Ok, in short this isn't a rewrite issue per-se. Your change is making the html file appear to be located under a sub-path so your relative paths in your HTML point to the wrong place. You could just change your relative links to absolute ones or change your layout (like with the DirectoryIndex suggestion) to ensure relative resources are actually where they appear to be.
SpliFF
hi plz check this http://74.220.215.241/~laborfa2/ and click on 'how it works' it will work but if click on right-top click on 'Los Angeles' that time it gives error..that time actual url will be http://74.220.215.241/~laborfa2/lf/main/com/search.php?area='Los Angeles'
how it works is a relative link href="how" the los angeles link is absolute href="/los-angeles/". You need to understand the difference. Absolute paths start at the begining of the web root and relative start at "where you are now". Using mod_rewrite can often confuse the "where am I" issue because your web browser and web server may not be in agreement (because you tricked the server).
SpliFF
<VirtualHost *:80>DocumentRoot /lf/main/comServerName http://74.220.215.241/~laborfa2ServerAlias http://74.220.215.241/~laborfa2RewriteEngine on#RewriteLogLevel 2#RewriteLog logs/rewrite.logRewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -fRewriteRule ^/(.*)(/?)$ /$1.php [L]RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})(/?)$ /profile.php?fairid=$1$2 [L] RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})/([a-z]*)(/?)$ /$3.php?fairid=$1$2 [L]</VirtualHost> it works finely on linux(htt.vhost) but when i paste it in .htaccess not works.so what change is required in above .
put that in your question or start a new one. comments are a poor location for code.
SpliFF
A: 

This is an URL resolving issue: Relative URLs are always resolved on a base URL that is the URL of the current resource if not other stated. So if you’re on /~name/foo and there’s a link pointing to ~name/bar (this is just a relative URL path!), it gets resolved to /~name/~name/bar. But if you would point to /~name/bar, it wouldn’t get resolved as it already is an absolute URL path.

So you can avoid that by either using absolute URL paths (those begin with a /) or by changing the base URL (see BASE HTML element). But the latter will affect any relative URL reference and not just those beginning with relative URL path.

Gumbo