Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a div in my index page whose id is "charts", so if I give "index#charts" as a value in anchor tag its works fine but now I have added .htaccess in my project for pretty urls.

Every other page is working fine, and I have write a rule in my htaccess that, if the url is "all-charts/chart-name/year" then redirect to "index#charts". My problem is that now its opening index page but not scrolling down to that div.

If I directly type "index#charts" in url its working.

Any idea what I am doing wrong?

Its kind of a complex structure, as I have a URL class which check all the request and returns the appropriate page. For example if I want to return the page named "blog.php" then in the link I will use "?page=blog" or for page "user_forum.php" i will give the href attribute value of "?page=user_forum". But now I am trying to change it to seo friendly url.

This is my htaccess code

RewriteEngine On

RewriteRule ^(backend)$ /backend/?page=index [NC,L]
RewriteRule ^(backend)/?page=([a-zA-Z0-9-_]+)$ /backend/?page=$1 [NC,L]

RewriteRule ^(blog)/([a-zA-Z0-9-_]+)$ /?page=read_blog&slug=$2 [NC,L]

RewriteRule ^(all-charts)/([a-zA-Z-_]+)/([0-9])$ /?page=index#charts [NC,L]

RewriteRule ^([a-zA-Z0-9-_]+)/([0-9]+)$ /?page=$1&cp=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9-_]+)$ /?page=$1 [NC,L]


And in anchor tag i used something like this
<a href ="all-charts/january-chart/2018">January 2018</a>

Thanks,
Anchal Bhargava

What I have tried:

I double check all the spelling in htaccess rule and in anchor tag. I tried changing the id. I added a footer message so there is a content beneath it
Posted
Updated 21-Mar-18 23:24pm
v3
Comments
Peter_in_2780 21-Mar-18 18:12pm    
Show us the exact rules you are using in .htaccess (and where the .htaccess file is). Use the "improve question" widget.
Patrice T 21-Mar-18 19:46pm    
Quote:"My link isn't working after adding rules in .htaccess"
In order to get help on your code, you need to show us that code.
Quote:"I double check all the spelling in htaccess rule and in anchor tag. I tried changing the id. I added a footer message so there is a content beneath it"
Show your code!

1 solution

The hash character # is a special character that will be converted. There is the RewriteRule Flags: NE|noescape - Apache HTTP Server Version 2.4[^] flag to omit the conversion:
Quote:
By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.
So try this:
RewriteRule ^(all-charts)/([a-zA-Z-_]+)/([0-9])$ /?page=index#charts [NC,NE,L]
 
Share this answer
 
Comments
Anchal Bhargava 22-Mar-18 6:11am    
Hello,

Thanks for the reply. I tried that but it doesn't solve the problem.

But from the link you gave me, if I use the "R" Flag its redirecting me to the desired location but url is changed to "?page=index#charts"
Jochen Arndt 22-Mar-18 6:24am    
I missed that you are not setting an URL but a parameter.

So the solution depends on how your script handles the parameter and forwards to the index page. It might help to enclose the parameter value by quotes:
?page='index#charts'

It might be necessary to update your script when doing so. But using the NE flag might be still necessary to avoid conversion. But it will also pass the '?' as it is.

On the other hand you might change your script to de-convert the parameter when not using the NE flag (replace %23 by #).

So you should check how your script is handling the parameter.
Anchal Bhargava 22-Mar-18 16:55pm    
Hello,

I print every request made, but when that rules executes it always stopped at index, I did put that in quotes so its showed 'index.

But when I directly type "index#charts" its working. So I am thinking maybe because "#" is a comment symbol in htaccess, that's why its not working because when I type that same url directly in the browser, its not meeting any rule and its passing directly.

But if that's the problem how do I escape "#" I tried adding "\" in front of it but it didn't work
Jochen Arndt 22-Mar-18 18:29pm    
The NE flag does exactly that.
Hower, as far as I know the hash character is treated only as comment character when at the beginning of a line (or when not inside a directive).
Using quotes will always treat it as normal character.

Can't you check the passed parameter in your script?
What happens when you enter the script URL with that parameter (url-to-page?page=index#charts)?
Anchal Bhargava 22-Mar-18 18:40pm    
Hello,

I tried using NE flag, but its not working I don't know why. Any how I found out that I can use "\%23" to escape "#" in htaccess and it works now. Although I have to use javascript to scroll down to that id now but its working.

Thanks for all your help and patience, with out your help it was almost impossible to do. Thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900