Redirect with Slim Framework - php

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

Related

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

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='';
}

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

How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

I have a page (example.com) with anchors at major sections (example.com/index.php#anchor, example.com/index.php#about, example.com/index.php#contact, etc.) as well as subpages (example.com/subpage.php).
Here's my .htaccess file:
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php
RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]
The subpage rewrite rule works great.
For the anchor link, including the "R" (redirect) flag changes the URL to example.com/index.php#anchor, but I want the URL to be example.com/anchor.
If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor.
So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript?
It's not possible to do that with URL rewrite. Because the browser scroll down when the # is part of the URL... not on the server side.
You can't do this with .htaccess. But you can do it with the following JavaScript code:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

Issue with # in URL with a Wordpress theme?

I am having a severe problem and have no clue about what is going on... I will specify the general issue which is causing multiple issues on this Wordpress powered portal.
Steps to reproduce:
Visit the URL: http://gamersgeographic.com/en/ (or any post on this site)
Append #abc or #anything to the URL
The URL tries to resolve for a second and magically deletes the "#" and instead changes to /abc or /anything , which of course does not exist and gives a 404 page not found.
Even if the local anchor with #abc exists, behaviour is the same.
Now, consider the case below:
Visit http://gamersgeographic.com/monster-hunter-diary-1/
Comment link appends a #comments or #respond depending on whether a comment is there or not.
Both the anchors exist on the single post page
Still it redirects after finding them, to /comments and gives 404
Direct URL with #comments works e.g. http://gamersgeographic.com/monster-hunter-diary-1/#comments works but when I change any base URL to #comments, it redirects to 404...
I have tried several combinations with Permalinks, so it is not a problem with that. I even wrote my own Comment link generator in php with just a plain
href="#comments"
but still no luck...
If you need any further information about any function's code in theloop.php or anything please let me know.
Thanks in advance !
Regards
The contents of .htaccess are as below:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This is not a PHP issue, it is Javascript: it is evident when you reproduce it, and you can test it by disabling Javascript and adding #comments at the end of the URL; it will work.
Now, I have done some work for you, and the culprit is a Javascript file aptly named hashchange.js. Look, for example, at this line:
function second_passed() {
if(current_page!=location.href {
get_page_by_hash(location.href);
}
setTimeout(second_passed,1000);
}
Which explains why you see it “working” for a second.
And here is the redirect:
jQuery(window).hashchange(function() {
var link = window.location.hash.replace("#", "");
get_page_by_hash(link)
});
Note that hashchange is a method for event handling available in jQuery Mobile.
<link rel="canonical" href="URL OF YOUR HOMEPAGE HERE>
add this in your header.php in <head></head> section and then try . it shouldn't be giving 404 error !
The way that page bookmarks are used is, as you know, the href="" of an anchor points to an #some-place. In order for this to happen #some-place must be the id of the element within the page you wish to go to.
For example:
http://gamersgeographic.com/monster-hunter-diary-1/#respond
should take you to the element with id="respond" in that page.
If the element with that ID doesn't exist you won't be able to travel to it, and may be the reason it results in a 404 Not Found. However, if the element does indeed exist on the page with the proper ID and it still redirects to a 404 then you may want to check your web server configuration to make sure it isn't filtering the # in some way.

after .htaccess url rewrite, cannot perform logoff in some of the url rewrited page

Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.

Categories