Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perfect url in address bar php, .htaccess, or javascript

This my .htaccess :

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    ErrorDocument 404 /?page=404

    # 1-level
    RewriteRule ^contact/{0,1}$  index.php?p=contact [QSA,L]
    RewriteRule ^displaydata/{0,1}$  index.php?p=displaydata [QSA,L]
    RewriteRule ^log-in/{0,1}$  index.php?p=log-in [QSA,L]
    RewriteRule ^log-out/{0,1}$     ?p=log-out [QSA,L]

    # newweb.com/contact# = newweb.com/contact/#
    #RewriteRule ^contact/(.*)?$ $1
    #RewriteRule ^displaydata/(.*)?$ $1
    #RewriteRule ^log-in/(.*)?$ $1
    #RewriteRule ^log-out/(.*)?$ $1


    ####
    # set URI to /index.php/200 if query string is id=200
    RewriteCond %{QUERY_STRING} (?:^|&)id=(200|1)(?:&|$) [NC]
    RewriteRule ^(index\.php)/?$ $1/%1 [NC]

    # set SECURED var to 1 if URI is /index.php/200
    SetEnvIfNoCase Request_URI "^/index\.php/(200|1)" SECURED

    # enforce auth if SECURED=1
    AuthType Basic
    AuthName "Login Required"
    AuthUserFile /full/path/to/passwords
    Require valid-user
    Order allow,deny
    Allow from all
    Deny from env=SECURED
    Satisfy any
    #####
    # all other cases
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .+ -  [L]
    </IfModule>

And this is Javascript:

 var reg_e=/#$/; if(!window.location.href.match(reg_e)) 
    { 
    	window.location.href = decodeURIComponent(window.location.href)+"#";
    } 

My url in the address bar :

newweb.com

added '#':

newweb.com/contact#

This is really good and works. But! My problem is:

I want this url

newweb.com/contact/#

to be

newweb.com/contact#

because the css won't load with the /. So I'd added RewriteRule ^contact/(.*)?$ $1

It works for contact/#

but when I click other link I will get:

newweb.com/contact/displaydata#

instead of :

newweb.com/displaydata#

How perfect url? Opinions and articles would be really welcome.

like image 997
Trí Đức Đào Avatar asked Nov 25 '25 02:11

Trí Đức Đào


1 Answers

Try this may be, might work sometimes. Put this first under your <head>:

<base href="/" />

Or use the src and href in such a way, that it is relative to the domain.

<a href="/some/path/to/file"></a>
<link rel="" href="/some/path/to/file" />
<script src="/some/path/to/file"></script>
<img src="/some/path/to/file" />

When you use any URL this way, you will be able to get it:

"http://example.com/my/path/#".substr(-2)  // "/#"

So keeping this as a thing, you can do:

var url = "http://example.com/my/path/#";
if (url.substr(-2) == "/#")
  window.location.href = url.substr(-2) + "#";
like image 148
Praveen Kumar Purushothaman Avatar answered Nov 26 '25 15:11

Praveen Kumar Purushothaman