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

二次信任 提交于 2019-12-11 17:33:44

问题


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.


回答1:


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]



回答2:


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



来源:https://stackoverflow.com/questions/30416595/hyperlinks-not-working-redirection-while-using-pretty-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!