Rewriting The URL With .htaccess - php

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.

Related

Unable to remove query strings from URL using .htaccess

My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?

I need a clean url without the parameters

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.

Rewritten URL shows 404 errors

I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.

mod_rewrite with ? in the original URL like YouTube

Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence.
I have the links as so
http://www.mysite.com/watch?v=Dxdotx3iT1
and want to redirect to
http://www.mysite.com/index.php?page=videos&section=view&v=Dxdotx3iT1
Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.
Any help would be appreciated.
You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:
RewriteRule ^watch$ index.php?page=video&section=view [QSA]
RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos&section=view&v=%1 [QSA,R=301,L]
None of these answers worked for me, so in the end I found through trial and error the following worked for me;
The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;
RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;
RewriteRule ^watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.

Query Strings & Mod ReWrite

I'm not too inexperienced with ReWrite (not a master either, though) so I was hoping somone might be able to help me.
RewriteRule ^$ index.php?page=home [NC]
RewriteRule ^adm$ index.php?page=adm_home [NC]
RewriteRule ^adm/stats index.php?page=adm_stats [NC]
Above is a snippet of my .htaccess file. As you can see, when someone visits http://www.example.com/adirectory/ it actually calls on index.php?page=home, similarly if someone goes to http://www.example.com/adirectory/adm/ it will still call index.php?page=adm_home within the "adirectory".
What I'm wanting to achieve is this: I want to be able to display alerts on my pages, and to do this I want to simply be able to add alert=n (where n is a number) and thus have the redirect as index.php?page=home&alert=n
However, I can't understand how this can be done, regex is confusing me. Seeking your help.
You can set the QSA flag to automatically append the originally requested query string to the new one:
RewriteRule ^$ index.php?page=home [L,QSA]
RewriteRule ^adm$ index.php?page=adm_home [L,QSA]
RewriteRule ^adm/stats$ index.php?page=adm_stats [L,QSA]

Categories