I have a query string which changes the content of the page, depending on what the user enters after the question mark in the url.
http://mysite.subdomain.com/?3
Would load content assigned to the ID 3
http://mysite.subdomain.com/?34
Loads content assigned to 34.
What do I need to enter into my .htaccess file so that when a user goes to
http://mysite.subdomain.com/1234
it loads content which would be loaded if the user entered
http://mysite.subdomain.com/?1234
Essentially I want to clean up the url and remove the question mark. Thanks. (I realise this is a basic question)
None htaccess
Since your javascript already processes the URL part, you don't need htaccess. The javascript takes the querystring, for instance ?OdfgzaBv2qY
, removes the questionmark and puts the leftover OdfgzaBv2qY
as ID in the iFrame.
Since the javascript is using window.location.search
the script expects variables to be set after the questionmark in the URL. So www.mysite.com/?3 will work but when you visit www.mysite.com/3 it won't, because the window.location.search
stays empty.
The reason why htaccess is no solution is because htaccess is being processed serverside, the javascript is being processed clientside. Even IF the htaccess is working, and you are being redirected to index.html?3
the javascript still sees the www.mysite.com/3 URL and not the final request. So basically: the server understands that the variable is set to ?3 in the background, but javascript sees what a user sees: a friendly URL without a questionmark.
Edit - can you give this a try:
<script type="text/javascript">
//GET DATA FROM URL ...http://www.mysite.com/YOUTUBE_ID
var DATA = window.location.pathname.replace("/", "");
window.onload = function() {
var YouTube = document.getElementById('YouTube');
YouTube.src = "http://www.youtube.com/embed/" + DATA;
};
</script>
window.location.pathname SHOULD take /3
and replace the /
. Whereas window.location.search takes the ?3
part and replace the ?
In case anyone needs a start up to htaccess
A basic .htaccess file for this would be:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
</IfModule>
Ofcourse index.php?id=1 should be replaced with whatever page your are using to execute the request.
A short explaination:
- the rule starts with the ^ and then looks up for a numeric value behind the base URL, so in case you have http://www.domain.com/42 the rule starts right after the last /
- The $ defines the end of the regex of the rule.
- index.php?id=$1 is the URL that has to be loaded (where $1 is parameter 1:
([0-9]+)
)
- the [L] is a flag "Last", which indicates that the htaccess should not look for other rules that apply
Note that this does not work for http://www.domain.com/49/ .. since the slash at the end is no match. This can be done by:
RewriteRule ^([0-9]+)/$ $1 [L,R=301]
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
this first looks if there's a slash, if so it redirects to the "slashless" url (R=301: Redirect 301 = permanent redirect), and then it matches the 2nd rule and executes the request.
Try this
RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?id=$1 [QSA,L,NC]