rewrite url only and stay on the same page - url-rewriting

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.

I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

Related

Migrating a CodeIgniter site to a different domain and moving the whole thing down one level in directory

I am working on a site, which is built with CodeIgniter.
I have never used the framework before...I've learnt a little bit in the last couple days but really cannot solve my problem.
I have downloaded the site and I want to upload it to my own domain so not to make changes to the live site before testing.
the original site is example.com.
I want to upload it to mysites.com/examplesite (this will be the base - I use this domain for all my clients' test sites).
I have moved all the files and the database is in place.
I have found the database configuration file and have successfully edited it.
I am having trouble getting the site to work, I think it must be because of the site now being down a directory level.
I have edited $config['base_url'] so that it is correct...I am pretty sure it is correct because I can now reach the homepage where I couldn't before editing that variable. But, i can only reach the homepage - when I click on a link to another page, I get 500 error Internal server error.
Does anyone have a quick fix for me?
Thanks!
Keep this code on your project's root .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /examplesite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Joomla not finding index.php after

I had turned Use URL rewriting to ON
Added some code in to the ht access file and this never worked.
I then turned off url rewriting and removed the code and now when I try to go to the website I am getting a 404 error.
I can still access /administration
the website is www2.daxtra.com
the page it should find is http://www2.daxtra.com/index.php/home
Does anyone have any ideas what has happened?
This was the code I added:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Thanks
Right, I've done a small test.
To get what you want, you need to following:
htaccess.txt (you don't need a .htaccess file)
Search Engine Friendly URLs set to On
Use URL rewriting set to Off
You finally need to ensure that your Homepage menu items it set to a specific component such as an article or something else, not an Alias
This will ensure that you get example.com/index.php/home
Hope this helps

How to keep mod_rewrite from recognizing directories

So, lately I've been dealing with an issue relating to mod_rewrite and it seems nobody is trying to do anything like it. Every question people have is about trying to exclude directories from the rewrite, when I want them to be included like any other.
For instance, assuming my root directory with .htaccess file in it is www.example.com/root/
When I type in made up directory, such as www.example.com/root/asdfasdf, I have my .htaccess file set to redirect me to www.example.com/root/index.php?url=asdfasdf without change what's in the address bar on my browser
However, in trying to do the same with a real directory, such as www.example.com/root/admin, it not only changes the url in the address bar but changes it to www.example.com/root/admin/?url=admin.
Can anyone explain to me what's going on. I've tried all kinds of different regular expressions and flags and the ones that redirect anything still cause this same issue. can I go to www.example.com/root/admin and still get redirected to the root folder while hiding that the query string is ?url=admin.
[UPDATE: additional information 11-30-2012]
Like I said, I've tried it will multiple different lines of code and come out with the exact same redirect issue, assuming the redirect doesn't just fail altogether and produce a 500 error. Here's one of my latest iterations, though, which has produced the issue of not ignoring direcotories.
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_FILENAME} !^(.\*\\.("png"|"jpg"|"gif") [NC]
RewriteRule (.\*?) index.php?url=$1 [QSA]
The rewrite condition is to keep the engine from rewriting if a picture is being requested (for css and img tags). I only didn't mention it previously because I have tried removing that line and it has made no difference.
I'm not exactly a master of mod_rewrite, though, so if you see any errors with anything I've written, please feel free to let me know.
It's not entirely clear from your question what you are trying to do and it would have been helpful to see what your .htaccess file actually looked like. However the following lines in an .htaccess file in the root folder:
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [L]
Will silently redirect requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php and will also do the same for real folders. So if the folder admin exists under root, then requests to http://www.example.com/root/admin will be silently redirected to http://www.example.com/root/index.php?url=admin
If however you wanted to serve up folders and files that actually exist, but rewrite requests for folders and files that do not exist, then you would need to adjust the rewrite like so
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [R=301]
This would still rewrite requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php, but for real folders and files, such as requests made to http://www.example.com/root/admin, the admin folder would be served up.
Hope this helps, but if you can clarify your question a bit then I can try and help again.

CDN Related : Rewrite Image URLs automatically from .htaccess

Recently I bought a CDN and set it up. In my site, the images are stored in a folder named 'images' and the Image urls are obviously linked in this manner. (*Ex : images/some_image.png*)
Since I want to use the CDN right way, I need to rewrite the urls without having to manually change each and every image path.
I tried an .htaccess code which was suggested for a similar problem
RewriteEngine On
RewriteBase /
RewriteRule ^images/(.*)$ http://cdn.mydomain.com/$1 [L,R=301]
But that didn't seem to work properly as all the images were linked improperly.
So I would like to know the changes in this code. Any response would be appreciated.
This should work:
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^images\/?(.*)$ "http\:\/\/cdn\.yourdomain\.com\/$1" [R=301,L]
However, please note that this is just a temporary solution!In order to get the max out of your CDN, you need manually point images to your CDN in order to save one HTTP for every image.

How to redirect url to another

OK...
I have setup things so that when the following page is requested (browser users and servers)
http://www.visualise.ca/?_escaped_fragment_=corona
the website returns the following the content of this (HTML snapshot)
http://www.visualise.ca/corona
Where 'corona' always change, it varies depending on the page the users or servers are requesting. It could also be
http://www.visualise.ca/?_escaped_fragment_=anne-au-cherry
redirecting to
http://www.visualise.ca/anne-au-cherry
Thanks
UPDATE: OK let me be more clear. I use AJAX to load my Wordpress post and they appear like this http://www.visualise.ca/#!/corona when loaded. But it's not crawlable by Google that request to serv them as http://www.visualise.ca/?_escaped_fragment_=corona so I modified Wordpress to do so. Now Google can crawl my page and index its content and accessing the HTML snapshot available at http://www.visualise.ca/corona.
The problem is that when I paste the http://www.visualise.ca/#!/corona link to facebook it seems to read the http://www.visualise.ca/?_escaped_fragment_=corona and is unable to read the content. But when I paste directly the http://www.visualise.ca/?_escaped_fragment_=corona link it works, it reads http://www.visualise.ca/corona (The HTML).
So I thought maybe if I could redirect http://www.visualise.ca/?_escaped_fragment_=corona to http://www.visualise.ca/corona it would solve my problem.
Here is the existing .htaccess file
#--- DH-PHP handlers ---
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/dispatch.fcgi
# 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
The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.
Use this rule: it will issue 301 Permanent Redirect from this kind of URL /?_escaped_fragment_=corona to /corona (where corona can be anything).
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L]
I guess your question is really "how do I do this kind of redirection?". So the answer is here:
There are three ways that I could think of, each with slight differences.
Doing the redirection server side
This basically means you set redirect headers in your response. In php, you could do this using the header function. It can also do a delayed redirect, in which case you need to worry about the contents of the page.
Doing it using client side using html's "http-equiv" meta tags. This way the page always gets loaded. Example here.
doing it via javascript. Thats you basic document.location.href thing. You need to figure out a way to pass the argument to javascript, or have your JS read it from the address url itself.
Since I've shown you 3 ways of doing this, I really hope that's what you're looking for :P
Update after seeing the comments:
The above methods will cause the URL to change. If you don't want the URL to change, but show the contents of that other page on your original page, you caould either do that using iframes (baaaad), or do the decent thing and set up URL rewriting.
:)
RewriteEngine on
RewriteRule \?_escaped_fragment_=(.*?) /$1 [L,R=301]
is it what are you looking for?

Resources