简体   繁体   中英

HyperLinks not working (redirection while using pretty url with htaccess)

Here is my php code(qs.php). This file contain pretty url links.

<html>
<head></head>
<body>
    <?php  
    $file = 'qs1'; //this is a php file qs1.php
    $id = '12345678'; //testing ID
    $complete_url = $file."/".$id;

    ?>
    <a href ="<?php echo $complete_url;?>"> This is a test link </a>

</body></html>

This link appear link this - http://localhost/qs1/12345678

QS1 is a php file (qs1.php).

QS1.php

<html>
<head>
<title>QS1 file</title>
</head>
<body>
<a href="#"><img src="images/blog/girl.png" class="img-circle" alt="" /></a>
<a href="#"><img src="images/blog/girl2.png" class="img-circle" alt="" /></a>
<a href="#"><img src="images/blog/girl3.png" class="img-circle" alt="" /></a>

</body></html>

Below is the htaccess code.

Options -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

Everything is working fine. My link is working properly. http://localhost/qs1/12345678 link is accessible by this. I am able to access the page. In my current page wherever i use this code( # ). I can say other links on that page.

<a href="#"><img src="images/blog/girl.png" class="img-circle" alt="" /></a>

It is redirecting to localhost only instead of passing the same to current page.

Not sure what i am missing.

I think you want : if file or directory not exit than redirect

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

I figured it out. This is not a htaccess or url redirection issue. It is JavaScript issue. # is used for jquery. My function was not working properly.

So, it was redirection to localhost. I found this link which help me understand what is going wrong. What does <a href="#" class="view"> mean?

Here is why it was not directing. I used # in jquery. Like below.

$(document).ready(function(){ 
$("#").click(function(){
    alert("Hello!");
});

$("#") isn't a valid selector. So, i will have to use anchor tags.

Below is the working code.

$(document).ready(function(){ 

    $(".vote").click(function(event) {
    event.preventDefault();
    alert("Hello!");
});

event.preventDefault() used because i am using

Hope this help someone.

I got help to find out the issue by DelightedD0D. You can see the complete answer on this link.

bind click event to anchor tags on my page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM