.htaccess redirect to another site - php

hello all i am building a url shortener site where we create smart short urls for any long url the short url looks like this reurl.in/anji89jd. whenever anyone clicks on this short url the user is directed to corrosponding long url which is stored in database and it is extracted by sql query on the index page everything is working good but i dont know how to direct the user to the long urls by htaccess
look if the short link is reurl.in/abcd then the index page is designed like this
reurl.in/index.php?r=abcd and then it directs the page to new site if i type this url everyhing works well
but reurl.in/abcd dosent works
please help me i know this can be done by .htaccess
i have tried like
RewriteEngine on
RewriteRule ^index/([0-9]+)/?$ index.php?r=$1 [L,QSA]

Try:
Options -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?r=$1 [L,QSA]

Related

.htaccess Redirect any file in directory to index.php of same directory

I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]

Codeigniter htaccess redirections not redirecting to desired url

I have a codeigniter website which I want to do seo on. So i used routes for sluggish urls rather than the codeigniter default having method name and indices.
With this a problem comes in play, three different urls it opens the same page ie.
1. https://www.example.com/seo-freindly-url/
2. https://www.example.com/index.php?/controller/method/42
3. https://www.example.com/controller/method/42
But I want only the first url to work, for others i want to redirect to the seo friendly one by only using htaccess redirections.
Please Help me in that.
One more thing i used htaccess 301 redirection to redirect one of my urls
Redirect 301 "/plan/index/42" https://www.example.com/services/seo
It works to some extent, whenever I open the url
https://www.example.com/plan/index/42
it redirects to
https://www.example.com/services/seo?/plan/index/42
but it has to be https://www.example.com/services/seo/
Thank you.
It's hard to test on my end, but you might try something like this:
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^controller/method/42$ /seo-friendly-url [L]
redirect 301 /plan/index/42 /services/seo
RewriteRule ^(system|application|cgi-bin) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Remove index.php from all URLs
RewriteRule .* index.php/$0 [PT,L]
/application/config/config
// This assumes you want to remove index.php from all URLs
$config['index_page'] = '';

URL rewrite skipping folder name using htaccess

I have a website for listing education centers.I have two doubts.
I have a URL for printing one college details like this
www.example.com/education/eduS.php?Main=colleges&Name=nameOfCollege
I rwrite this url like this
www.example.com/education/colleges/nameOfCollege
my htaccess is
RewriteRule ^(.*)/(.*)$ eduS.php?Main=$1&Name=$2 [L,NC,QSA]
My problem is that
I want to skip the folder name eduaction from my url so as to get URL like this
www.example.com/colleges/nameOfCollege
How it is possible. I want to get Main and Name as GET parameters.
my Second problem is that , I have another url for listing colleges name in one page. The url like this
www.example.com/education/edu.php?Main=colleges&Category=Engineering-colleges
I rewrite like this
www.example.com/education/colleges/Engineering-colleges
my htaccess
RewriteRule ^(.)/(.)$ edu.php?Main=$1&Category=$2 [L,NC,QSA]
but this shows little bit confusion. The above two URL have same number of GET parameters. How can I handle this problem.
I also need to skip same folder name education from the second URL. Anyone help me
Use this code in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ education/eduS.php?Main=$1&Name=$2 [L,QSA]
This allows you to have your URLs like: /colleges/nameOfCollege

mod_rewrite with name replacing ID

I have a page on my website that dynamically displays content. The URL structure is mywebsite.com/giveaway/giveaway.php?id=(any number)
I wish to change that dynamic URL into a static/friendly URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id.
In my .htaccess file found in my root folder i have the following:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
The current .htaccess file removes .php and also redirects and changes a URL on my site from
mywebsite.com?page=number to mywebsite.com/page/number
I know you have to get the name corresponding to the ID in php from my database and use that, but im not sure how to write the code in the .htaccess file or how to use and pass the results from the database. Thanks for any help!
I don't know if that is what you mean, but you can't connect the rewrite engine, which is part of the underlying Apache server layer, to the database, which has to be accessed by php.
So you can't rewrite the "name-of-giveaway-corresponding-to-id" to the id directly, you need to rewrite it to something like giveaway.php?nameofgiveaway=(name of giveaway) and then search the database for that string.
Your way to go is to add a rewrite rule like
RewriteRule ^giveaway/([^/]+)$ giveaway.php?nameofgiveaway=$1 [L,QSA]
(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway'].

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories