Use .htaccess to hide .php and query name - php

I have been searching all over google and stackoverflow, and have been finding a large number of answers but none seem to work for me.
Basically what I want is to rewrite my url localhost/index.php?page=1 to localhost/1, while still keeping the query alive.
Now, I have managed to remove the .php with the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But removing the Query simply does not seem to work.
Please help me out.

Your rule doesn't match what you're trying to do. You're telling it to take 1 or more of anything and make it into blah.php (like http://localhost/blah -> http://localhost/blah.php).
You're looking for something like RewriteRule ^([0-9]+)$ index.php?page=$1 [NC,L].
By the way, your language is a bit backwards. You're not rewriting index.php?page=1 to localhost/1; you're rewriting localhost/1 to index.php?page=1 (well, really 1 to index.php?page=1).

Related

.htaccess filename to url

I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!

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.

Url rewrite to takeout

Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
www.example/account/product.php?Sef=shoes&pid=3
To something like this
www.example/account/product.php?Sef=shoes/pid/3
If you can help me. It will be lovely. Thanks
For your second problem you don't need htaccess for that just parse $_GET['Sef'] on your server side.
As per php extension try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

PHP Mod Rewrite Sitemap Software Not Working

I was looking to make the URLs on my site a bit more SEO friendly after reading a few sites about it. Basically I wanted old URLs that looked like
/showarticle.php?articleid=1
/showstory.php?storyid=1
to instead go to these urls:
/article/1/article-headline
/news/1/news-headline
I got this working by using the following mod_rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^article/([0-9]+)/([^/]*)$ /showarticle.php?articleid=$1 [PT] [L]
RewriteRule ^news/([0-9]+)/([^/]*)$ /showstory.php?storyid=$1 [PT] [L]
However, since implementing those sitemap software and sites don't seem to be working properly. phpsitemapNG finds just 1 page, and XML-sitemaps.com finds some but not all. On the other end of the scale though, GSiteCrawler and AuditMyPC find more than there are by replacing the headline part of the url with other stuff such as:
/news/1/contact.php
/news/1/virtualnetwork.php
/news/1/compare.php
These are all pages on my site but not in the news or article section and there's no links to any of these pages in the form that they're picking them up. Is this something to do with my rewrite rule or just something that sitemap generators do in general?
NOTE: The OP originally added this to their question. I have moved it to an answer to be in line with site guidelines.
Just a quick note to say that this issue has been sorted out. I just had to use absolute URLs to stop this from happening.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^/article/([A-Za-z0-9-]+)/(.*)/?$ showarticle.php?articleid=$1 [NC,L]

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?.

Categories