I have a simple SLIM application that responds to all requests to sent it in an API-like way for example the following url:
http://example.com/pages/subdirectory/slimApp.php/products
As you can see, the url to the slim app is absolute and pretty long but I have tried using Apache2 mod_rewrite to shorten the url as shown below
RewriteEngine on
RewriteBase /pages/
RewriteRule ^slimApp/(.*) subdirectory/slimApp.php/$1 [L,NC]
The code above rewrites to the main route of the app which is basically a 404 error page and acts like no request has been specified. When I access the app with an absolute url, it works as normal but if I try to access /products from the shorten url version, it fails with a slim 404 page.
Basically what I need is to translate the following and still be able to handle requests like shortened_url/products, shortened_url/something_else, etc. For-example:
http://example.com/pages/subdirectory/slimApp.php/products
to
http://example.com/slimApp/products
Bear in mind that /products is dynamic, any help is greatly appreciated.
Related
I am trying to rewrite URLS so that /foo -> index.php.
index.php is powered by codeigniter 3.
bar.php is a stand alone php file.
This is a snipped from my .htaccess:
//Test 1. Bar.php displayed. This works as exptected.
RewriteRule ^/?foo1/?$ /bar.php [QSA,L]
//Test 2. Redirects succesfully to index.php. Works as expected.
RewriteRule ^/?foo2/?$ /index.php [R=301,QSA,L]
//Test 3. Goes to codeigniter 404 page and does NOT display the homepage.
Does not work as expected
RewriteRule ^/?foo3/?$ /index.php [QSA,L]
Why does Test 3 not display as expected? There is something in my codeigniter code that doesn't work when trying to rewrite URLS. Any ideas how to fix this? Is there a workaround?
Full disclosure: the long term aim is to be able to have a URL with structure like this:
example.com/foo1/foo2/foo3/?query1=xxxx&query2=yyyy
rewrite to
example.com/bar/bar1.php?queryA=foo2&queryB=foo3&query1=xxxx&query2=yyyy
Can this be "easily" done within the codeigniter framework (route.php) rather than htaccess? From a development time perspective, just getting it to work via .htaccess would be the preference.
Take a look at https://gist.github.com/keithmorris/3023560
This includes a fairly standard htaccess file that I use on all my projects. You then use the CI routing functionality to define where different bits go.
Bear in mind with CI you can pass additional elements in a url such as example.com/foo1/foo2/foo3/query1/query2.
I'm using Angular JS along with cakephp version 3.x. For pretty URL for angular js state routing, i used the following code to achieve.
Added in config,
$locationProvider.html5Mode(true);
Added base in Pages/home.ctp (cakephp file. It's like an index.html)
<base href="/">
It actually works until i reload the page. Also, I've some controller and actions like example.com/contact, example.com/work etc.. which serves a normal php page.
For example :
Before pretty url :
I've a state url example.com/#/profile
Also I've a normal php page example.com/profile
This actually works until i added a html5Mode.
After adding pretty URL :
Angular JS state url becomes : example.com/profile (that's fine)
But if i reload that, it generally throws error as it's actually looking for general MVC page, not angular.
I'm sure that there's is conflict with Angularjs - Cakephp. Plus I've some authentication like only the logged in users can only access certain actions, if not they will be redirected to homepage.
I tried putting server side code in .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It doesn't work.
I have installed SVN and use it to maintain access to my repositories via the http protocol (http://localhost/svn/project_name/ maps to the physical location /var/www/repos).
I also have Apache installed for my web application (with a physical location of /var/www/web_app). This can be access via http://localhost/, rendering a default action (Presenter: Homepage, Action: default - this yields index.php).
However, when I want to view the Contact Page (Presenter: Contact, Action: default - yielding http://localshots/contact) it says:
NOT FOUND The requested URL /contact was not found on this server.
The error doesn't look like it is a framework error (bad code for routing in the framework or something similar). It looks like there is a conflict between in .htaccess or the apache config.
I'm using a PHP framework (called Nette, but I think it's not important what framework is being used: I think the problem lies between the routing and apache configs).
So, it seems that you have no rewrite rule for the url you are requesting.
2 possibility here, you write a new rule like :
RewriteRule ^[/]*contact$ index.php?page=contact [L]
The second is to modify the url you want to reech like http://localhost/contact.php, then create a php file a the root of your project.
EDIT :
You can also do it like this :
RewriteRule ^[/]*contact$ contact.php [L]
So you have a beautiful url and redirect directly to your file
I am trying to enable clean urls with Angular JS inside my Laravel 4 app. When I tried the required thing normally without laravel 4 refering to this url.
http://www.yearofmoo.com/2012/11/angularjs-and-seo.html, I was able to get the thing to work. Even ?_escaped_fragment_= was changed to snapshot/* folder and picked the content.
But I am not able to do the same in Laravel 4. I am not sure how to do this.
I am trying to define a route like ?_escaped_fragment_=/* and redirect it to some controller but that doesn't work.
Can anyone please help.
Escaped fragment is a query parameter, not a route, there for, in the root route Route::get('/', 'RootCtrl');, you can check for $_GET['_escaped_fragment'] presence, and if it is there, return an HTTP 302 redirect to the corresponding file in the snapshots dir.
While this will work, it is not the perfect solution, since some bots might index the path of the snapshots file instead of the original one, and since doing it using the Laravel framework is not correct in the first place.
The best choice of course, is to rewrite any request that contains an _escaped_fragment_
query parameter to it's corresponding path, in the nginx\apache configuration, prior to the configuration of your Laravel app, this way those requests won't event reach the Laravel router.
I have co-authored an Angular SEO plug-and-play solution, using PhantomJS & Mongoose integrated web server, to pre-render any Async JS code, and server it as raw HTML.
The server configuration aspects are also explained in the README.md file, please note that if you would like to use static snapshots, just change proxy-pass to rewrite(in order for the right URL to get indexed by the bots).
Hope that helps.
Example using simple rewrites with nginx:
if ($args ~* "^_escaped_fragment_=(.+)") {
set $path $1
set $args '';
rewrite ^.*$ /snapshots/$path last;
}
EDIT:
Apache, for your request (some words of advice: move to nginx).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment=(.+)$
RewriteRule ^.*$ /snapshots/%1 [QSA,L]
</IfModule>
How does instagram.com pass the username variable like "instagram.com/username" or like
instagram.com/floydmayweather
without using the $_GET function and it does not turn out looking like this
instagram/index.php?username=floydmayweather
Use a URL rewrite command in your HTTP server. There are many examples out there for both Apache and nginx.
The rewrite rule happens at the server level before it hits your code. This means the URL doesn't actually have to get modified before your code receives it.
The way I do it is I configure Apache/nginx to send all URLs that do not match an existing file (so that static files like images, js and css still work) to my index.php file. Then in the index.php file I parse the URL to determine what page type to load and what data.
In your example, they would grab the last token off the URL, know that it would be a user's name in URL format, look up that user in the database and build the page accordingly.
This is where something like a front controller or URL router comes in to play in most frameworks. In index.php I would map each URL, based on its components, to a class that would then handle the actual page building.
Here is more info on the rewrite modules;
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://wiki.nginx.org/HttpRewriteModule
Some quick Googling will show you many examples for how to configure this.
Your index.php file can examine the $_SERVER array to determine the URL that has been requested. In this situation, the explode() function is your friend, for parsing the URL and checking its components :)
The Rewrite engine will be a perfect solution, for example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Rewrite engine - A rewrite engine is software located in a Web application framework running on a Web server that modifies a web URL's appearance. This modification is called URL rewriting. Rewritten URLs (sometimes known as short, fancy URLs, search engine friendly - SEF URLs, or slugs) are used to provide shorter and more relevant-looking links to web pages. The technique adds a layer of abstraction between the files used to generate a web page and the URL that is presented to the outside world.
Usage
Instead getting URL with extenstion link (.php / html etc..)
www.stackoverflow.com/index.php
You will get URL Without extenstion
www.stackoverflow.com/index