.htaccess - it is possible to hide all /#some from sample.com/#some - php

There's some way to hide all redirect from URL,
or maybe just show my domain on every click
I've tried to use this in .htaccess but the sample.com/#some is not gone just (.php) is hide.
RewriteEngine On
#remov php exten
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ /%{REQUEST_URI}.php [NC,L]
#redirect root to sub
RewriteRule ^((?!).*) /%{REQUEST_URI} [L,NC]
RewriteRule ^((?!).*)/#{REQUEST_URI} [L,NC]
any good idea?

The hash in the URL is client-side only. So it is not sent to the server, so you would never change it via .htaccess file.
However, you could use some javascript codes to remove it like this:
$('a').click(function(){
removeHash();
});
function removeHash () {
window.location.href = window.location.href.split('#')[0]
}
Do not forget to modify $('a') with your ids if you need. Currently the code above is removing all #somes when any link is clicked.
Updated:
Or you can completely prevent from page refresh incase you want to use onclick attribute of a element. In that case, use below function instead.
function removeHash () {
window.location.hash='';
}

Related

htaccess: RewriteRule for URL rewriting

I have a link that looks like this
http://www.example.com/artistmusic?address=JKanyomozi
I would like to change this URL to
http://www.example.com/JKanyomozi/music
I've only been able to do this by adding music as a parameter instead of adding it to the new URL as a prefix.
In short, i've turned this URL ...
http://www.example.com/artistmusic?address=JKanyomozi&req=music
... using this rule ...
RewriteRule ^([^/]*)/([^/]*)$ /artistmusic?address=$1&req=$2 [L]
... into ...
http://www.example.com/JKanyomozi/music
But the problem with this is that for other pages like the one for the videos, this rule redirects to the same page http://www.example.com/JKanyomozi/music instead of http://www.example.com/JKanyomozi/videos
The videos page's original URL is something like this ...
http://www.example.com/artistvideos?address=JKanyomozi
Which rule can I use to change the URL above to
http://www.example.com/JKanyomozi/videos
This applies for other pages like artistevents, artistphotos and artistbiography
Thank you in advance!
Try this :
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]

mod_rewrite issues and php

I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??
Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links

Redirect with Slim Framework

I have the following code:
$app->get('/category/:name', function($name){
//the render files are found under the templates folder
header('Location: searchPage.php?crs_category=$name');
});
The problem is that when I type /category/business its just lands on blank page. I do not want to render the page because I can't exactly render searchPage.php?crs_category because it is view as a template and there's a work around to include to variable. I just want to go directly to that page while keeping the url clean. Essentially redirect to this page in the background with the url remaining that.
Thanks in advance.
Assuming you're using Apache as a webserver, couldn't you just rewrite the URL in .htaccess:
RewriteRule ^category/(.*)$ searchPage.php?crs_category=$1 [L,NC,QSA]
..or if you want a real redirect
RewriteRule ^category/(.*)$ searchPage.php?crs_category=$1 [L,NC,R=301]
PS If you insist on doing it in Slim, you could try
$app->get('/category/:name', function($name) use ($app) {
$app->response->redirect('/searchPage.php?crs_category='.$name, 303);
});
source: http://docs.slimframework.com/response/helpers/#redirect

mod_rewrite rewriterule?

hello i have an anchor on my site. by calling my site the url will be:
http://site.com/page.php
by clicking a button the url will display an url that i would like to hide:
http://site.com/page.php#1
now i have found out that i can change this by using mod_rewrite tool. i tried this rule without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]
for calling the page as html or php and to hide the #1 behind the .php
RewriteRule ^(.*)$ ./php#1=php$1
i really would appreciate if there is someone who could give me advise on how to solve this. thanks a lot.
You cannot achieve this via mod_rewrite.
URL after # are not passed through server.
If you still want to achieve this, you may want to look into doing it via JavaScript:
Check this out
JavaScript:
function scrollHere() {
// Scroll to the anchor
//(you may need to look for some script to achieve this)
return false; // to prevent URL overwriting
}
One such script is this jQuery.scrollTo.
The hashtag is never sent to the server, thus you simply cannot create a rule, that based on it.
You can do something like this to remove # tag.
Your text.
So when you write "javascript:void(0);" in href attribute of a tag and thats it.
HTML
<a href='page'>Home Page</a>
<a href='page/1'>Page 1</a>
<a href='page/2'>Page 2</a>
<a href='page/3'>Page 3</a>
Rewriting as
RewriteRule ^$ page.php [L]
RewriteRule ^([a-z]+)/?([0-9]*)/?$ page.php?index=$2 [L]
And check for index in php
<?php
if (isset($_GET['index']))
{
$pageNo =$_GET['index'];
}
//based on page NO display contents
?>

PHP/Htaccess - problem with a loader

I have an htaccess file in the root that redirects every request of a page to a specific file:
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
Now the redirect is easy in the loader.php
include($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['url']);
I just include the URL,admin/index.php for example.
If I leave the code as is the loader will include the file and print the correct HTML, but it will not load any CSS or JS scripts. If I put echo "test";
just before the include, the loader will load the CSS file. It's something that is killing me. Do I have to specify something in the HTTP header?
I already tried putting <base url="" /> in the header of index.php with no result,
but another strange thing is that with Chrom if I inspect the page and click on the link I'll see the right CSS.
Update 1
I printed the headers_list();. I noticed one thing - when I print an echo in headers_list, an array shows the content-type, so I tried to add it on my own with the header() function but with no result. Still working on it.
Update 2
I've noticed another thing; if I put a <style></style> tag with some CSS it will work fine, but if I use the <link /> tag it doesn't. This doesn't make any sense.
You have to send the correct MIME headers for css and javascript (and images etc.).
Easiest is to just let apache handle those requests. Put all the CSS, JS and images in a folder named 'assets' or something and change the htaccess to
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]

Categories