Rewrite site with first slash to index - php

I'm sorry about the title but I can't find a good one to explain what I try to do.
I have my website : http://domaine.com .
The index.php file in it redirect to http://domaine.com/views/index.php .
Now I want to Rewrite Rule so that any url like :
http://domaine.com/something display the site http://domaine.com so that :
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
etc.
EDIT : I'm working on localhost so it's supposed to:
localhost/formation/php => localhost/formation/php/views/index.php
localhost/formation/java => localhost/formation/java/views/index.php
localhost/formation/ruby => localhost/formation/ruby/views/index.php
EDIT : The content of my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ views/index.php [L,QSA]
When I access to localhost/formation/ruby/ for exemple it's working but after the loggin it look like it's always redirecting from the home page to login page to home page etc.
EDIT : I create a repertory called "ruby" and create in it a htaccess file with that content.
RewriteEngine On
RewriteRule ^(.*)$ ../$1 [L]
And it does exactly what I want. So I want to find a way to do that without having to create the repertory "ruby".

http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(php|java|ruby)/$
RewriteRule ^.*$ /%1/views/index.php
I suppose by "redirect" you mean "serve as". In case if you want a 301 redirect, use the appropriate flags for RewriteRule, e.g. [R=301,L].
Wrapper
In case if you are trying to implement a wrapper script which is supposed to handle different request URIs, you should process the requests with a single script and pass the URI path parts as query string parameters. For instance:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(formation|another_type)/(php|java|ruby)/$
RewriteRule ^.*$ /wrapper.php?type=%1&lang=%2
With this configuration, you can handle requests like http://your-site.com/formation/ruby/ with a single script. You can access the query string parameters using the $_GET superglobal variable, e.g. echo $_GET['lang'];.
Ultimately, you can process all requests with a single wrapper:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/wrapper.php
Within the wrapper you will have to examine the contents of the $_SERVER superglobal. The REQUEST_URI and REQUEST_URI values are particularly useful.

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]

301 htaccess Redirect that will force?

Trying this again because I realize how very poorly I worded my original post.
I have a page, Product1.php, that is dynamic for populating my products from a database.
This is the rubric of my SEO-friendly urls:
http://www.example/category/subcategory/model_name-model_id/product_id
This is the original url with query string that it rewrites to:
http://www.example.com/Product1.php?=Product_ID=1
This is the rewrite function I have in htaccess that makes this happen:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/.*/.*/([0-9]+)$ Product1.php?Product_ID=$1 [L,QSA]
However, if a user manually-types in http://www.example.com/Product1.php?=Product_ID=1 in the address bar, this is the one they still see. Moreover, it's the same for the search engine bots, which is dividing the value of my pages.
How do I write a 301 redirect that will force the user and bot to see the SEO-friendly url only, regardless of how they access the page? I have researched for days and various solutions I have tried give me only 404 or 500 errors.
Please help. Thanks in advance.
**EDIT: OK, looks like I can't invoke RewriteMap because I don't have access to my host's config files. (Need to upgrade our account to do so and employer is unwilling.) So will have to do a Rewrite Rule for each individual page, which is unfortunate but doable.
But still need to find out how to force the redirect with causing 404 or 500 errors. Anyone?
I used a little trick adding an "Internal" parameter to a rule responsible for main rewrite, and the check of this parameter in the 301 redirect. The solution with environment variables is also allows to process requests where the parameters may be presented in any order.
RewriteEngine on
# perform collecting parameters from query string to an environment variables
RewriteCond %{QUERY_STRING} Category=([^&]+)
RewriteRule . - [E=category:%1]
RewriteCond %{QUERY_STRING} SubCategory=([^&]+)
RewriteRule . - [E=subcategory:%1]
RewriteCond %{QUERY_STRING} Product_ID=([^&]+)
RewriteRule . - [E=productid:%1]
#...etc...
# give a right 301 redirect
RewriteCond %{REQUEST_URI} Product1.php
# make sure it's not an internal redirect
RewriteCond %{QUERY_STRING} !^Internal=1(.*)$
RewriteRule . /%{ENV:category}/%{ENV:subcategory}/%{ENV:productid}/? [L,R=301]
# now rewrite rule with the additional "Internal" parameter to label it as an internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)/([^/]+)/([^/]+)
RewriteRule .* Product1.php?Internal=1&Category=%1&SubCategory=%2&Product_ID=%3 [L]
Thus, the following request
/Products1.php?Category=cat1&SubCategory=subcat1&Product_ID=321
will result in 301 redirect to
/cat1/subcat1/321/
and the appropriate page will be shown.
One more way is to remove the "Internal" trick and use another name of the script, Products2.php for example. I.e. you need something to distinguish external request from internal rewrite. to avoid infinite loop ("too much redirects" error).

Rewrite url which redirects to page on webserver

I am trying to get a page with a query string to redirect to a nicer looking url then get that url and transfer it back to the original query string but without redirecting (i.e. without changing the url)
At the moment I am getting a redirect loop (for obvious reasons) but I was hoping for a way to stop this.
This is my code in my htaccess file
#rewrite search querystring
#/search/'apartment'/2_bedrooms/price_0-500000/town_W4/development_18,SW5/
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) /search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
/search/$1/$2_bedrooms/price_$3-$4/town_$5/development_$6 [R,L]
RewriteRule ^(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6? [R,L]
So what it is meant to do is:
user has been taken to:
http://www.domain.com/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
This page is the actual page on the server where the data is coming from, however I want the user to see.
http://www.domain.com/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
Is it possible to do this without a redirect loop.
Thanks for your help
EDIT I'm thinking it could be done with the rewrite flags but I'm not sure, I'm quite new to the Rewrite Engine
Edited:
Here is a complete (and working) solution for you:
RewriteEngine On
# User gets here:
# http://localhost/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
# He is explicit redirected to here:
# http://localhost/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
# Internally, apache calls this:
# http://localhost/search.php?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC,PT]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
RewriteRule ^search/(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6/? [R,L]
It assumes you put .htaccess in server root and that there is a file search.php in root too.
Original:
I think you can use PT and QSA Rewrite Rule flags (http://httpd.apache.org/docs/current/rewrite/flags.html) in your first rule
Use PT for server-side redirection (it will not change the URL for the user/browser, but will for your server-side scripts)
Use QSA if you wanna carry the query while doing this redirection
You can redirect all requests that don't target an existing file to a specific php-script, for example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,QSA]

htaccess and php - redirects and pretty urls

I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
You are able to link to users like this domain.com/username and that's nice.
Then I have different pages like
index.php?page=forum&id=1
index.php?page=someotherpage&id=1&anotherid=5
index.php?page=3rd
... and so on. I want them to look something like this:
domain.com/forum/23/title-of-the-thread
domain.com/page2/id1/id2
... and so on.
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
I was thinking about creating a file that checks the URL,
you actually have that file, it's index.php
if it matches any pages, and users and so on. Then it will redirect with a header location.
that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.
Just change your rule to more general one
RewriteRule ^(.*)$ index.php [L,QSA]
You basically have two options.
Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
Note every possible route (url rewrite) you have in the .htaccess
I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
you said
I was thinking about creating a file that checks the URL, if it
matches any pages, and users and so on. Then it will redirect with a
header location.
While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in
an extra HTTP roundtrip for the client, leading to slower page loads
the "pretty URL" won't stick, the browser will show the URL you've redirected to
losing link-juice (SEO)
This can be done entirely with htaccess or php
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2
read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

PHP & MySQL url rewriting question

Can I rewrite a url by using the value 6 from ?cat for example, and checking my MySQL database for the value 6 category name html using PHP & MySQL if so how?
Current url.
http://localhost/index.php?cat=6&sub1=8&sub2=24&sub3=81
New search friendly URL displayed in browser.
http://localhost/html/basics/forms/select-tag/
Check out mod_rewrite.
You need to open up the .htaccess file(if it does not exist, create it) and include the following lines. Note that RewriteEngine On should be only written once.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^html/basics/forms/select-tag/([0-9]+)/?$ index.php?cat=$1 [NC,L]
Yes. Basically you will want to pass the entire url through a router script using mod_rewrite. The .htaccess file should have something like this:
RewriteEngine On
RewriteBase /
#if it's an existing directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
#...or an existing file
RewriteCond %{REQUEST_FILENAME} -f
#serve it up
RewriteRule ^(.+) - [PT,L]
#else, direct to index.php
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Then, in index.php you can have something like this:
$request = explode('/', $_GET['url']);
Which would have all your clean url segments in the $request array. Once you have those values, you can connect to the database, find what the url represents, and output the page. If it's not found, you can send a 404 header with a "Page not found" message.
header('HTTP/1.0 404 Not Found');
echo '<p>Page not found</p>';
So that's the basic clean url technique. If the url is the earlier, messy one, you would just add some logic to check for that, and redirect to the correct clean url:
if(isset($_GET['cat'])){
//put some logic here
header("Location: http://localhost/".$the_clean_url);
}
This basic technique should solve your problem with some tinkering.

Categories