Removing whitespaces or %20 in url using htaccess - php

hi guys can you help me here im having a little trouble here im tring to use the htaccess to remove all 20% in my url and replacing it with hyphen I manage to get rid the other 20% in between the words Acer,Liquid,S1,S510
here is my url /localhost/gadgets/product/Acer-Liquid-S1-S510%20Mobile
As you can see there is one %20 in last part, how can I remove it
And here is my htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /gadgets/
Options -Indexes
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1-$2 [NC,L]
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance guys

Try this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /gadgets/
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]
RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The first rule removes all the whitespaces in a loop (using [N]) so we don't have to specify multiple RewriteRules doing it one by one now.
The next rule (with {ENV:Redirect} condition) is optional and is used to reflect the use of hyphens on the client's browser as well so that any bookmarks created link to the correct non-whitespaced version of the URL.

Related

HTACCESS Not Picking Up The Path

I'm using HTACCESS in my website but I'm facing a small problem with one of the files/links.
My link is http://localhost/photos/view/1465574353
My HTACCESS is:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?category=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
When I follow the link I am not redirected anywhere at all, yet when I follow http://localhost/photos/view.php?id=1465574353 I am shown the page.
Anyone know the reason why this may be?
** If I change it to
RewriteRule ^view/([0-9]+)/([\w-]+)/?$ view.php?id=$1&title=$2 [NC,L,QSA]` and visit my page at `localhost/photos/view/1465574353/title
I am shown the correct page!
FYI: I'm using the same format for my other section, which looks like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?section=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^read/([0-9]+)/([\w-]+)/?$ read.php?id=$1&title=$2 [NC,L,QSA]
http://localhost/photos/view/1465574353 is already matched by
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]
view - ([\w-]+)
1465574353 - ([0-9]+)
and therefore not handled by the final RewriteRule.
If you want it handled by the more specific (view) rule, you must swap the last two, because the rules are tried in order.
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1&currentpage=$2 [NC,L,QSA]

URL Rewrite Optional Parameter

I'm trying to get a my original url which would have been properties.php?bed=1&page=1&perpage=12&area=testarea to /search/1/1/12/testarea. But the last query string 'area' is optional and so could be blank.
I have this so far below, the problem is if I add an extra ([^/]+)/ such and
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4
and then don't supply an area in the url, it fails to work correctly. Is there a correct way to do this where the query sting can be optional?
Also another problem I'm having is I would like this
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4
to be /search/rooms/ not /search-rooms/ but if I put an extra / after search it again doesn't work correctly and the page reads the wrong data from the URL.
This is the full version
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule property/([^/]+)/? pages/show.php?id=$1 [NC,L]
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/? pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteRule search-rooms/([^/]+)/([^/]+)/([^/]+)/? pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^www.studentlettingsagency.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^studentlettingsagency.co.uk$
RewriteRule ^(.*) http://www.stla.co.uk/$1 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
Any help would be great!
You can use the following rule :
RewriteCond %{REQUEST_URI} !^/search/rooms
RewriteRule search/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/properties.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteRule search/rooms/([^/]+)/([^/]+)/([^/]+)/?([^/]*)?/?$ pages/rooms.php?bed=$1&page=$2&perpage=$3&area=$4 [NC,L]
RewriteCond is important here ,as it tells the first rule not to match the uri /search/rooms

Can't use "?" in URL because .htaccess

I have the following code into my .htaccess:
RewriteEngine On
Options -MultiViews
Options +FollowSymlinks
RewriteBase /testUrl/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
Then I catch the params using $_GET['page'] method.
This works:
localhost/testUrl/param1/param1
This DOES'NT works
http://localhost/testUrl/param1/param1?newparam=test&nextparam=test1
BUT THIS WORKS:
http://localhost/testUrl/param1/param1&newparam=test&nextparam=test1
I need to make working the 2nd example... with /param1/param2?newparam=etc
Any tips? Thank you so much guys
Thats because your rewrite rule doesnt include the QSA flag witch tells apache to append query string.
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [QSA,R=301,L]
to read more about rewrite mod flags go to - http://httpd.apache.org/docs/2.4/rewrite/flags.html

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

htaccess RewriteEngine

I have an url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55
and I want it to be like
http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
The line Options +FollowSymLinks is optional if already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things:
RewriteEngine On only needs to called once, though this may not be causing any problems
I also have RewriteBase / after my RewriteEngine On line
My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L], which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
you should use only one RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
Try again
Do you want you url to be /bid/55 or /bookmark/55? because you have written it as if it is going to be /bid/55...
Anyway, your .htaccess should look more like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
...without the multiple RewriteEngine on directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L] flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^/$) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf, you don't use them in .htaccess files.
If you want your urls to be /bookmark/, just replace bid with bookmark.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link may make things clearer: http://corz.org/serv/tricks/htaccess2.php

Categories