Panchoa
Panchoa

Reputation: 147

Wordpress .htaccess redirection issue

Recently, I moved from a static website to a WordPress made website. Everything works perfectly except one thing.

My company uses players to display content and the players call an url like this to get the content :

https://mysite.fr/#thecontentpage.html

This url is from the old website. As the new website is on WordPress now, the url is a more "classic" one :

https://mysite.fr/thecontentpage.html

The best solution would be to make the players calling the new url but it's impossible. So I thought about doing a redirection with the .htaccess but it doesn't work.

Here's my .htaccess :

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Redirect 301 https://mysite.fr/#thecontentpage.html https://mysite.fr/thecontentpage.html

Even with the redirection, when I call https://mysite.fr/#thecontentpage.html I land on the main page and not the page I want.
What can I do to have my redirection working ?

Upvotes: 1

Views: 153

Answers (2)

Panchoa
Panchoa

Reputation: 147

As I couldn't make the redirection in PHP or with the .htaccess, I just used Javascript to made the redirection.

I insert this script in the header.php file from my child theme :

<script type="text/javascript">
    var url = window.location.href;
    var racine = url.substring(0, url.lastIndexOf("/"));
    var page = url.substring(url.lastIndexOf("/")+1);

    if(page == "#old_url.html"){
        document.location.href = racine + "/new_url.html";
    }
</script>

As this file is located in the child theme, I'm not bothered by the updates on the main theme. My problem is solved then !

Upvotes: 0

Daniel C
Daniel C

Reputation: 2155

You are out of luck here as the hashtag is never sent to the server since it's an in-page fragment. It is only ever used by the browser. Therefore the .htaccess file will never see it, and you can't write a rewrite against it. So, you will have to come up with a unique way to redirect using JavaScript before WordPress loads. Most likely you'll need an index.php page that handles redirects and then sends anything not caught in your rule straight through to WordPress.

UPDATE As mentioned, you can't do it with "WordPress" as it won't work do to the server never getting the hash part. You can, however, use JavaScript. Place this code just inside your <head> tag:

<script>
    var $hash = window.location.hash.substring(1);
    if( $hash.length > 0 ) {
        window.location = $hash;
    }
</script>

Clearly you will want to really alter this simple code to do more enhanced checking and validation so that example.com/#whatever doesn't try to redirect... but this is a start for you. You could have an array of "whitelist" redirects and check $hash against that array... just a thought.

Upvotes: 1

Related Questions