Mod Rewrite SEO urls and R=301 - .htaccess

I want my URL to look like as following and also inform search engines the URL has permanently changed.
www.example/shop/12/title
I achieved this by using the following
RewriteRule ^shop/([^/]*)/([^/]*)$ /shop.php?prod=$1 [L]
but, I'm stuck on the Permanent Redirect URL.
When I try the following
RewriteRule ^shop/([^/]*)/([^/]*)$ /shop.php?prod=$1 [R=301,L]
Links like www.example/shop/12/title are redirected to www.example/shop.php?prod=12
This should stay as the seo friendly link.
Any Help?

Since your new urls contain a title that is not in your original 'ugly' urls, you cannot redirect from within mod_rewrite. It is not possible to summon information from thin air in mod_rewrite.
The best you can do is adding the following in the top of shop.php:
if( strpos( $_SERVER['REQUEST_URI'], "shop.php" ) !== FALSE ) {
$id = $_GET['prod'];
$title = getTitleById( $id );
header( "Location: /shop/{$id}/{$title}", TRUE, 301 );
exit();
}

Related

How do I set a proper .htaccess file for 301 in bludit?

I have 12 old url .html an d want to redirect to new url without .html like that /blogpost.html > /blogpost. What I'm writing in .htaccess
Use Redirect:
Redirect permanent /blogpost.html /blogpost
Instead of twelve Redirect statements, one for each of your old urls, you could stuff them into a single, though probably a little confusing RedirectMatch, too:
RedirectMatch permanent ^/(blogpost|oldpage|ancienturl|longago)\.html$ /$1
See http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect and http://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch for details.

how do i display the user nickname instead of the ID on the page profile [duplicate]

Normally, the practice or very old way of displaying some profile page is like this:
www.domain.com/profile.php?u=12345
where u=12345 is the user id.
In recent years, I found some website with very nice urls like:
www.domain.com/profile/12345
How do I do this in PHP?
Just as a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?
According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
I recently used the following in an application that is working well for my needs.
.htaccess
<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>
index.php
foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
// Figure out what you want to do with the URL parts.
}
I try to explain this problem step by step in following example.
0) Question
I try to ask you like this :
i want to open page like facebook profile www.facebook.com/kaila.piyush
it get id from url and parse it to profile.php file and return featch data from database and show user to his profile
normally when we develope any website its link look like
www.website.com/profile.php?id=username
example.com/weblog/index.php?y=2000&m=11&d=23&id=5678
now we update with new style not rewrite we use www.website.com/username or example.com/weblog/2000/11/23/5678 as permalink
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Create a .htaccess file in the root folder or update the existing one :
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What does that do ?
If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.
2) index.php
Now, we want to know what action to trigger, so we need to read the URL :
In index.php :
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Now in the profile.php file, we should have something like this :
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
Simple way to do this. Try this code. Put code in your htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*)/ profile.php?u=$1
RewriteRule profile/(.*) profile.php?u=$1
It will create this type pretty URL:
http://www.domain.com/profile/12345/
For more htaccess Pretty URL:http://www.webconfs.com/url-rewriting-tool.php
It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, www.example.com/profile/12345 and then apache chops it up using a rewrite rule making it look like this, www.example.com/profile.php?u=12345, to the server. You can find more here: Rewrite Guide
ModRewrite is not the only answer. You could also use Options +MultiViews in .htaccess and then check $_SERVER REQUEST_URI to find everything that is in URL.
There are lots of different ways to do this. One way is to use the RewriteRule techniques mentioned earlier to mask query string values.
One of the ways I really like is if you use the front controller pattern, you can also use urls like http://yoursite.com/index.php/path/to/your/page/here and parse the value of $_SERVER['REQUEST_URI'].
You can easily extract the /path/to/your/page/here bit with the following bit of code:
$route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
From there, you can parse it however you please, but for pete's sake make sure you sanitise it ;)
It looks like you are talking about a RESTful webservice.
http://en.wikipedia.org/wiki/Representational_State_Transfer
The .htaccess file does rewrite all URIs to point to one controller, but that is more detailed then you want to get at this point. You may want to look at Recess
It's a RESTful framework all in PHP

Is it possible to do http://www.something.domainname.com where something could be a variable?

I would like for any user come to redirect to their page on my site after they login with .htaccess.
For example, "username" login and redirects to:
http://www.username.domainname.com
That will be handled internally like this:
http://www.domainname.com/users.php?user=username
Yes it is by doing a redirect.
If you are writing in PHP, you can do this:
<?php
$username = $_GET["user"]; // Note that this is user input (which could be malicious)
header("Location: http://www.{$username}.domainname.com/");
?>
But, as you have not specified a language, that is just one example. You can easily search for how to do a redirect in whatever framework you are using and there should be good examples.
If you want to go the other way, try a URL Rewrite.
Here's a very basic one that should work:
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domainname\.com$
RewriteRule ^.*$ http://www.domainname.com/users.php?user=%1 [L,R]
The rule looks for any request that matches www.(something other than .).domainname.com and sends back a redirect request for http://www.domainname.com/users.php?user=(something other than .)

301 Redirect in htaccess. Matching Page IDs

I would like some help creating an htaccess 301 redirect for the below type of url.
In total there's around 500 or so products but rather than write a redirect for every url, which would be very bulky and time consuming, I'm hoping there's an easier way that I haven't yet found to create a kind of regular expression match?
OLD: http://www.example.co.uk/test-product-name-slug/prod_233.html
NEW: http://www.example.co.uk/test-product-name-slug-233.html
The new URL can be accessed by browsing to ..... example.co.uk/-223.html ...... which then rewrites to ..... example.co.uk/test-product-name-slug-233.html
So it would appear I need a way of detecting if the incoming visitor is coming to a url that cotains prod_id and redirecting to -id
I hope that all makes sense.
Hopefully this is what you're looking for. It only matches 1 directory in
/some-product_name-random/prod_9393.html => /some-product_name-random-9393.html
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-A-_]+)/prod_([0-9]+)\.html$ /$1-$2.html [R=301,L,QSA]
Regex and Parameters Explained
([a-zA-A-_]+) matches product name $1
prod_([0-9]+) matches product id $2
[R=301] 301 permanent redirect
[L] stop .htaccess script (may be removed, but usually good practice for specific rules when using multiple rules for different scenarios)
[QSA] keep query string domain.com/somepath/page.html?querystring=value&otherstuff (?...)

htaccess Redirect 301 : mysite.com/sub/ to mysite.com/sub/home

my homepage url is like this http://mysite.com/sub/
I just want it to redirect to new url something like this http://mysite.com/sub/home?lang=en
here's my code
Redirect 301 /sub/ /sub/home?lang=en
Problem/error:
the new url becomes like this http://mysite.com/sub/home?lang=enhome
there's unnecessary home concatinated after en
how can I removed this? Or is there something wrong with my code?
don't know there's might be already same question like this
This is because the Redirect directive "connects" 2 path nodes, and you've got one inside the other (/sub/home is inside /sub). For example, if the directive looks like this:
Redirect 301 /a /b
This means when someone requests http://mysite.com/a/foo/bar they get redirected to http://mysite.com/b/foo/bar. What happens when you get redirected to /sub/home is that you get redirected again because /sub/home matches the pattern /sub, and the home gets appended, thus /sub/home?lang=enhome.
You can try using RedirectMatch instead, which doesn't "connect" path nodes:
RedirectMatch 301 ^/sub/?$ /sub/home?lang=en
Or mod_rewrite:
RewriteEngine On
RewriteRule ^/?sub/?$ /sub/home?lang=en [L,R=301,QSA]

Resources