.htaccess / mod_rewrite

I actually want to run ~name/how

instead ~name/how.php

.

I made the following changes to .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 work like:

~name/~name/how

      

And every time the link is clicked, it will be added ~name

to the ex url. http://ip/~name/~name/~name/serach

...

Can you tell me what is wrong with .htaccess?

0


a source to share


2 answers


You are using relative paths, not absolute paths. Now your pages think you are in the ~ name / path. You can install <base href>

on your pages.

You could simplify this by using "DirectoryIndex index.php" to match / whatever /whatever/index.php

This might help: How to use apache mod_rewrite rewriterule without changing relative paths



EDIT: short answer. You should consider using absolute paths for your menus and php to prevent problems when you start nesting deeper. Your relative paths are only valid when you are at two levels (i.e. ~ Laborfa2 / search /)

To be honest, you are associated with riots, so you are confused. I highly recommend creating a very simple test site so you get your paths organized correctly. Use absolute paths and relative paths as needed. Remove the base href as it just confuses you. Once you have this, try rewriting again, bearing in mind that your relative paths may need to be updated if the path depth changes via rewriting.

+1


a source


This is a URL resolution issue: relative URLs always resolve to the base URL, which is the URL of the current resource, unless otherwise noted. So if you're on /~name/foo

, and theres a link pointing to ~name/bar

(this is just a relative URL path!), It gets permission /~name/~name/bar

. But if you point to /~name/bar

, it won't resolve as it is already an absolute url.



So, you can avoid this by using absolute URL paths (the ones that start with /

), or by changing the base URL (see HTML BASE element ). But the latter will affect any relative URL link, not just the initial one with a relative URL.

0


a source







All Articles