Clean AND "dirty" url - php

I made a htaccess for clean urls that is processed by PHP after, however for my search option I want a youtube-like '?q=searchquery'.
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)/$ index.php?d=$1 [L]
That's my htaccess and it works for all urls but 'http://www.website.com/search/?q=searchquery' doesn't. Now I've tried other rewrites such as these
RewriteRule ^(.*)/\?(.*)/$ index.php?d=$1&$2
RewriteRule ^(.*)/\?q=(.*)/$ index.php?d=$1&q=$2
They dont work.
I also had a look the entire URI using the [R] flag and everything from the question mark doesn't show.
RewriteRule ^(.*)/$ index.php?d=$1 [L]
Using the url: http://www.website.com/search/?q=searchquery displays: http://www.website.com/search/
Edit:
So how do I do both? \o/

Just use the QSA (Query String Append) flag:
RewriteRule ^(.*)/$ index.php?d=$1 [L,QSA]
If the URL is called with a query string, the query string is appended to the generated URL.
PS: You can't match a query string within the rewrite rule. The query string is stripped off, before the rules are matched.

Related

Cleanning urls with htaccess

i am trying to use RewriteRules to get clean urls using my HTACCESS file.
here is what i have so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The above code takes a url that looks like this company.com/about.php and turns it into company.com/about/ so all my links url are like this "/about/" i didnt add the .php because of the rewrite rule.
what i am trying to do now is add a rule that will clean my url when parameter is passed. for example
company.com/about/?profile=member_name i want it to look like company.com/about/member_name
i have tried the two rewrite code below but it doesn't work.
RewriteRule ^([a-zA-Z0-9]+)$ /our_work.php/ [L]
RewriteRule ^/our_work.php([^/\.]+)/?$ ?project=$1 [L]
please keep in mind that my file extension is already being striped from the url.
Please help
Thank you in advance
Apache provides documentation on the use of RewriteRule that you should read before proceeding.
In this particular case, you can rewrite /about/{member_name} to /about.php?profile={member_name} by implementing this rule:
RewriteRule ^about/([a-zA-Z0-9]+)(/)?$ about.php?profile=$1 [L]
The rule states: The requested URL must begin with about/ and be followed by letters and numbers (match #1) and may be proceeded by an additional ending forward slash (match #2); and it will be substitued with a URL about.php with profile query string's value as match #1. The L flag indicates that any rules that follow this rule should be ignored.
Attempting to "cascade" rules will not work with your current set of rules because the /about/{...} is not matched by the first rule.

Remove Trailing Slash from Specific URL using HTACCESS

How to force remove trailing slash from specific URL using htaccess,
For example :
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
and ignore other URL such as https://blahblah.com/about/ or https://blahblah.com/contact/ etc
Because the trailing slash is part of the querystring you can't just rewrite it, you have to extract the querystring minus that final / and then redirect to the page you're on with that match appended.
To do this you need to match on the pattern in the RewriteCond with %1 (see this answer for reference) and append that to the %{REQUEST_URI} (thus removing the original querystring) - like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^.*$ %{REQUEST_URI}?%1 [L,R=301]
RewriteCond %{QUERY_STRING} ^(.*)/$ <-- make sure you've got that / at the end here in your conditional - it ensures that only querystrings that end with / are redirected so you don't end up in a horrible recursive loop.
You can try this:
RewriteCond %{QUERY_STRING} (.*)/$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]
This will redirect every request containing query strings only. In your case this:
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/
to
checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1
But not this https://blahblah.com/about/ and https://blahblah.com/contact/
I would advise trying to find out why that trailing slash is there to begin with first. As CD001 said in the comments it could be that your PHP code is doing something wrong.
If however you do need a htaccess solution to remove the slash then the below should work for you.
RewriteCond %{QUERY_STRING} ^(.*)/$
RewriteRule ^(.*)$ ?%1 [L,R=301]
This has been tested at http://htaccess.madewithlove.be/ and works for your example URLs.
if you mean an internal routine
RewriteCond %{QUERY_STRING} ^(checkout\=[^\&\s]+\&id\=\d+\&item_options\[price_id\]\=\d+)\/
RewriteRule ^ %{REQUEST_FILENAME}?%1 [L]

remove everything after comma in url and redirect in htaccess

I'm trying to add some rewrite rules to my htaccess file and I'm having a bit of trouble with a few bits.
I've currently got the following set up (bits are from different sites so I'm sorry if it's not consistent!):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?part=([^&]+)\ HTTP/
RewriteRule ^search\.php$ http://test.dev/part/%1? [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L]
This allows me to go to test.dev/search.php?part=foo and it rewrites/redirects the url to test.dev/part/foo - this works fine and shows me the correct content.
However, I can also go to test.dev/search.php?part=foo,bar and it rewrites/redirects the url to test.dev/part/foo,bar - even though this shows up the correct content, this is not what I want the url to look like.
Is there a way to take into account what's in the url after 'part' and if it finds a comma, remove the comma and anything after it?
I'd still need the 'foo' and 'bar' to be read by my code and show the correct content for those two options but for the url to show test.dev/part/foo (as foo was the first one in the url).
Replace your with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+search\.php\?part=([^&,\s]+) [NC]
RewriteRule ^ /part/%1? [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L,NC]
UPDATE: To capture string after comma into query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+search\.php\?part=([^&,\s]+)(?:,([^&,\s]+))? [NC]
RewriteRule ^ /part/%1?q=%2 [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L,NC,QSA]

PHP and Apache: hiding php parameters as directories; second parameter not accessed?

I'm currently using the apache code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Categories + Page:
RewriteRule ^([a-z\-]+)/([a-z\-]+)$ /index.php?category=$1&page=$2
RewriteRule ^([a-z\-]+)/([a-z\-]+)/$ /index.php?category=$1&page=$2
# Categories:
RewriteRule ^([a-z\-]+)$ /index.php?category=$1
RewriteRule ^([a-z\-]+)/$ /index.php?category=$1
which turns my urls from:
example.com?category=software&page=mobile
into
example.com/software/mobile/
Which works fine, and the php file is able to $_GET['category'] fine; however, for some reason, it does not recognize the $_GET['page']; it just results in it being empty every time.
I'm not the best when it comes to Apache code, so would anyone be able to tell me where I went wrong with it?
The code I'm using for php to get the variables is simply:
$category = $_GET['category'];
$page = $_GET['page'];
Also, if anyone could help me optimize it so that it could work for any number of parameters, that would be even better.
Thanks!
RewriteRule ^([a-z\-]+)/([a-z\-]+)$ /index.php?category=$1&page=$2 [PT,L,QSA]
RewriteRule ^([a-z\-]+)/([a-z\-]+)/$ /index.php?category=$1&page=$2 [PT,L,QSA]
# Categories:
RewriteRule ^([a-z\-]+)$ /index.php?category=$1 [PT,L,QSA]
RewriteRule ^([a-z\-]+)/$ /index.php?category=$1 [PT,L,QSA]
Adding QSA may help
When the replacement URI contains a query string, the default
behavior of RewriteRule is to discard the existing query string, and
replace it with the newly generated one. Using the [QSA] flag causes
the query strings to be combined.
Consider the following rule:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
With the [QSA] flag, a request for /pages/123?one=two will be mapped
to /page.php?page=123&one=two. Without the [QSA] flag, that same
request will be mapped to /page.php?page=123 - that is, the existing
query string will be discarded.

How can I transform a query string generated by a search form into new URL without the query string?

I have a search form that on submit generates something like this url:
http://mydomain.com/index.php?find=some+text
And I'm aiming to make it look like:
http://mydomain.com/find/some+text
How can I do this with .htaccess ?
So far I have this:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} &?find=(.*)&?
RewriteRule ^index.php$ find/%1? [R=301,L]
RewriteRule ^find/(.*)$ index.php?find=$1 [L]
This works if the query (i.e what I search for) contains only numbers, letters or underscore, but I want to make it capable of search for anything including spaces and other characters!
So, it seams that the 404 error was being cause because of some configuration in the web server that did not permit URLs to have + (plus sign).
It would break at the first + and try to find a file by that name.
Having that sorted out the rewrite rules were like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /*.index\.php\?find=([^&\.]+)?\sHTTP
RewriteRule ^/?index.php$ /find/%1? [L,R=301]
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,NC]
Thanks for your help!
Shouldn't something like this work?
RewriteRule ^find/([^/]*)$ /index.php?find=$1 [L]
You need to match against the actual request and not the URI because the URI gets rewritten via your second rule. This causes a redirect loop.
To redirect the browser:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?find=([^&\.]+)(&([^\ ]+))?
RewriteRule ^/?index.php$ /find/%1?%3 [L,R=301]
To internally rewrite it back
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,QSA]

Categories