I need a clean url without the parameters - php

Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C

A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .

It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.

Related

Creating a WordPress-like permalinks rewrite/redirect using PHP and .htaccess, is it possible?

I've been working on a module for a PHP-based application that would allow users to create custom pages. These pages have a URL structure as follows:
yourdomain.com/page.php?url=random-slug
I would like to have these URLs rewritten to use a more SEO-friendly approach:
yourdomain.com/random-slug/
In addition, there should two (2) redirects that accompany this rewrite:
yourdomain.com/page.php?url=random-slug => yourdomain.com/random-slug/
yourdomain.com/random-slug => yourdomain.com/random-slug/
Would this be possible? If so, what would the .htaccess (Apache) file look like to accomplish this? I have tried the following for the rewrite:
RewriteRule ^([^/]*)?.php$ ./page.php?url=$1 [L,NC]
Unfortunately, the above also seems to break other links on the site as well. I'm not exactly sure why yet but was hoping someone could point me in the right direction.
Thank you for your help!
Edit:
I was able to find a way to accomplish this with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]
RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
Hopefully this will help anyone else with a similar request :)

Redirect Urls With Parameters Using Htaccess

I did a "site:" command in google to view the indexed urls and found a lot of urls with parameters all pointing to the same page.
Its a static site, but about 5 years ago it was a Wordpress site, which may be how those links were created and indexed by Google. The 3 main links I found are as follows. They all look like these 3 but with different id's.
http://example.com/index.php?option=com_banners&task=click&bid=41
http://example.com/?option=com_content&view=article&id=86&Itemid=201
http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7
My question is how do I redirect these links, with all of the different parameter ids, to their corresponding pages. The first two go to the homepage and the 3rd one goes to a /phlebotomy-jobs page.
So, what I'm looking for is, for example, this url: http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7 to redirect to http://example.com/phlebotomy-jobs
Basically removing everything from the ? on.
Well, not sure if it's the "best" way, but this, for example, works:
RewriteCond %{QUERY_STRING} option=
RewriteRule (.*) /$1? [R=301,L]
RewriteEngine On
RewriteRule ^index.php?$ /path/page.php [R=301,L,QSA]
RewriteRule ^$ /path/page.php [R=301,L,QSA]
RewriteRule ^phlebotomy-jobs?$ /path/page.php [R=301,L,QSA]
The QSA RewriteRule Flag appends the query string.
https://httpd.apache.org/docs/2.4/rewrite/flags.html
Edit:
Since you're not concerned with retaining the query strings:
RewriteEngine On
RewriteCond %{REQUEST_URI} (^/index.php$|^/$|^/phlebotomy-jobs$)
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1? [R=301,L]
Note: This method prevents passing any query strings to either of those two pages.

Rewriting The URL With .htaccess

Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.

Mod_ReWrite regex unknown variables count?

i store uploaded files at /storage/ this way
public-adam-luki-uploads-123783.jpg
park-hanna-adel-propic-uploads-787689.jpg
the '-' count unknown because it slice the pic description
i want my users to be able to access it as
http://site.com/public/adam/luki/uploads/123783.jpg
http://site.com/park/hanna/adel/propic/uploads/787689.jpg
i think it is the same problem here
mod_rewrite with an unknown number of variables
but i can't do it because i'm new to mod_rewrite module
i hope you can help me guys with the right rewriterule
The question you link to doesn't actually do what you are trying to do (although the principle is the same) what they do is convert the url to GET variables.
If all you want to do is convert / to - then you can use a simple rewrite rule that will run in a loop:
ReWriteRule ^(.*)/(.*)$ $1-$2 [L]
There are of course a few caveats to that...
Firstly, even if you are trying to get to a real directory/file the rule will still switch out / and - and leave you with a 404. You can get around that by adding conditions; to stop it rewriting real files:
RewriteCond %{REQUEST_FILENAME} !-f
You would do better however to limit the matches to only images (jpgs):
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*)/(.*)\.jpg$ $1-$2.jpg [L]
Preferred Solution
ReWriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)/(.*)uploads[-/](\d+)\.jpg$ images/$1-$2uploads-$3.jpg [L]
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)$ storage/$1 [L]
This solution requires you to use urls like:
http://site.com/images/park/hanna/adel/propic/uploads/787689.jpg
The pseudo directory images means you can be sure that the url is actually one that you want to redirect and it doesn't break other images/links on your site.
The above rules take a url (like the example above) and transforms it like so:
images/park/hanna/adel/propic/uploads/787689.jpg <--- Original
images/park-hanna/adel/propic/uploads-787689.jpg
images/park-hanna-adel/propic/uploads-787689.jpg
images/park-hanna-adel-propic/uploads-787689.jpg
images/park-hanna-adel-propic-uploads-787689.jpg
storage/park-hanna-adel-propic-uploads-787689.jpg <--- Final

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

Categories