So I created a simple mvc project and uploaded In bluehost, but I'm having an server error problem which I guess is because the .htaccess in my project. I've done some research if mod_rewrite is enabled in bluehost, and found that by default it is enabled. But I still get the server error. I hope someone can help me out thanks.
Here my .htaccess inside my public folder:
RewriteEngine On
RewriteBase /streaming/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Here is the error I get:
server error 500
Your error would indicate that you are trying to perform an operation or function on an array when it is expecting a string. For example, if you have something like:
echo $_GET;
This will not work, because $_GET is an array and the echo command only works with strings and other primitive data types.
Instead, you should either specify the index that you want such as:
echo $_GET['url'];
or if you want all the data in the array you can either loop through each element of the array and process each string one at a time such as:
foreach($_GET as $key => $value){
echo "<p>The value of $key is $value</p>";
}
or use a different method that does support the entire array such as:
print_r($_GET);
Of course this is all guesswork without knowing what your code on line 94 actually says.
Related
I tried doing searching and trying to understand how to do a redirect with (multiple) query strings but I didn't have luck. I'm hoping someone here can help me understand this issue :)
I'm working on this ecommerce shop and people are searching the ecommerce search input for content located in a different CMS. For example, the word "returns". This isn't a product in the ecommerce system so of course it returns an error for the results (no products found).
My idea was simply to manually redirect those quieres to the proper landing pages in the CMS.
Here's an example of the URL for "return" on the ecommerce system:
http://www.domain.com/catalog/search.php?mode=search&page=1&substring=return
And here's where I would like to send people:
http://www.domain.com/catalog/Returns.html
Any thoughts on how to do this? Thanks in advance!
Solution
The way to do this is as Phil suggested; but with a few (small) modifications:
RewriteEngine On
RewriteCond %{QUERY_STRING} substring=returns? [NC]
RewriteRule . /catalog/Returns.html? [L]
RewriteCond %{QUERY_STRING} substring=shipping [NC]
RewriteRule . /catalog/Shipping.html? [L]
N.B. In the event you only want to remove one parameter see the Additional Information and Explanations below.
N.B. For more strict matching see Where & becomes a problem below.
Explanation
Background
The best way for me to explain the difference (between the above and Phil's original) and why you were having a problem is to explain what is going on...
RewriteCond %{QUERY_STRING} substring=returns? [NC] checks the query string for instances of the regex that follows it in this case substring=returns?*.
The [NC] flag simply means to match upper and lower case letters.
*Clarification: The regex(substring=returns?) means substr=return is matched literally with or without an s.
Problem
If the condition is met (i.e. the regex pattern is matched in the query string) then the rewrite rule is triggered. This is where the problem lies...
Given the URL: http://example.com/?substring=returns
The original rule:
RewriteRule . /catalog/Returns.html [L]
Rewrites the URL leaving the query string in place, like so:
http://example.com/?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
...and so on until limit is reached...
Side note: The [L] flag stops the .htaccess file from going through any more rules but it doesn't stop it looping again.
Solution
The solution then is to overwrite the query string (since we no longer need it) you can do this simply by adding a ? to the end of the RewriteRule:
RewriteRule . /catalog/Returns.html? [L]
N.B. In the event you only want to remove one parameter see the Additional Information and Explanations below.
N.B. For more strict matching see Where & becomes a problem below.
Resources
The following resources may come in helpful in the future:
.htaccess flags
http://httpd.apache.org/docs/current/rewrite/flags.html
Regular expressions
http://www.regular-expressions.info/ - Check out the tutorials section
Additional Information and Explanations
Where & becomes a problem
RewriteCond %{QUERY_STRING} &substring=returns? [NC]
In the above the regex means to match the characters &substring=return with an optional s appended to it.
So it would match the following as expected:
http://example.com/?var1=somvalue&substring=return
http://example.com/?var1=somvalue&substring=returns
http://example.com/?var1=somvalue&substring=return&var2=othervalue
http://example.com/?var1=somvalue&substring=returns&var2=othervalue
Which is fine and given the original query string wouldn't be a problem, however, if I were to navigate to the page and write in the parameters in a different order, the & wouldn't necessarily be there and therefore it wouldn't match (when it should):
http://example.com/?substring=return&var1=somevalue
http://example.com/?substring=returns&var1=somevalue
Simply getting rid of it (as I did) would solve this problem, but it doesn't come risk free.
RewriteCond %{QUERY_STRING} substring=returns? [NC]
If you were to introduce a new parameter secondsubstring for example it would match when it shouldn't:
Good Match > http://example.com/?substring=return&var1=somevalue
Good Match > http://example.com/?var1=somevalue&substring=return
Bad Match > http://example.com/?secondsubstring=return&var1=somevalue
To solve this potential issue you could do the following:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?
The above will match:
http://example.com/?substring=return&var1=somevalue
http://example.com/?var1=somevalue&substring=return
But won't match:
http://example.com/?secondsubstring=return&var1=somevalue
One more potential problem is that the expression would match:
http://example.com/?substring=returning&var1=somevalue
http://example.com/?substring=return%20television&var1=somevalue
My understanding, again, is that this wouldn't be a problem in the given situation. However if it were to be a problem you could do:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?(&|$)
The above checks that the character following return/returns is either an & signalling the end of the variable and the start of a new one or the end of the query string.
Rewriting one parameter
In some circumstances as Phil pointed out it may be preferable to only remove one parameter at a time and leave the rest of the query string untouched.
You can do this, quite simply, by implementing capture groups in the RewriteCond and outputting them in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?(&.*)?$ [NC]
RewriteRule . /catalog/Shipping.html?%1%2 [L]
Rewrite explanation
You use %N to insert capture groups from the rewrite condition and $N to insert capture groups from the rewrite rule.
So in this case we redirect to:
/catalog/shipping.html?(RewriteCond Group1)(RewriteCond Group2)
/catalog/Shipping.html?%1%2
The [L] flag - as previously - stops the processing of any rules further down the .htaccess file
Regex explanation
^(.*&)?substring=returns?(&.*)?$
^ Start of string
(.*&)? First capture group
Capture any character . 0 or more times *
Followed by an &
The ? makes the entire group optional
substring=returns? Matches substring=return literally with an optional s
(&.*)? Second capture group
Capture an &
Capture any character . 0 or more times *
The ? again makes the group optional
$ End of string
[L] flag vs [END]
For completeness sake...
The [L] flag stops the .htaccess from going over any more rules further down the .htaccess file.
The [END] flag stops the rewrite process completely.
To illustrate with an example:
while(TRUE){
if(condition1){ continue; }
if(condition2){ continue; }
if(condition3){ continue; }
if(condition4){ continue; }
}
while(TRUE){
if(condition1){ break; }
if(condition2){ break; }
if(condition3){ break; }
if(condition4){ break; }
}
In the above code blocks the [L] flag acts like a continue statement in that it skips the rest of the code block and starts again. Whilst the [END] flag acts as a break statement and stops the loop entirely.
If we were to replace the [L] flag with [END] in Phil's original answer then it would work. With the caveats mentioned in the Where & becomes a problem section above.
RewriteEngine On
RewriteCond %{QUERY_STRING} &substring=returns? [NC]
RewriteRule . /catalog/Returns.html [L]
RewriteCond %{QUERY_STRING} &substring=shipping [NC]
RewriteRule . /catalog/Shipping.html [L]
etc.
Would something like that do the job for you? Note that 'returns?' means 'return' or 'returns'. Are you limited to one search term at a time, or might customers type in a phrase? I think & is safe to use there, but it's possible it's not.
Don't forget to do this stuff ahead of any commands to rewrite Returns.html to Returns.php, do SEO, etc.
I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.
With the following url http://www.example.com/de/here/ I want to remove the "de" directory (or whatever may be in front of the "here" directory, if anything even is in front of it) so a user is directed to http://www.example.com/here/ instead, which is a directory that does actually exist.
The url could even be something like http://www.example.com/it/here/ or any other combination of 2 letters.
The url could also just be http://www.example.com/here/ in which case I don't want anything removed at all.
I have searched for a solution here but cant seem to make anything work correctly so any help would be much appreciated.
You can use this kind of htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
Example of redirections caused by this code :
http://www.example.com/de/foo/ => http://www.example.com/foo/
http://www.example.com/de/ => http://www.example.com/
http://www.example.com/it/bar/ => http://www.example.com/bar/
http://www.example.com/FR/baz/ => http://www.example.com/baz/
Please note you won't be able to access the language (de, it, fr...) anymore.
Another point, be careful with this kind of url (the redirection will be executed twice) :
http://www.example.com/de/go/ => http://www.example.com/go/
http://www.example.com/go/ => http://www.example.com/
EDIT
Now I've got more details, here is an htaccess you can you to remove the language for specified folders :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/here/(.*)$ here/$1 [L,R=301]
RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]
I made a little mistake (I started a new php call within an existing php call - oops) and managed to have google start crawling a whole bunch of urls that look like this:
http://www.mydomain.com/folder/parameter/%3C/?php%20echo%20writelink();%20?%3E
I've fixed the sourcing call, but my attempts to have .htaccess rewite the page calls to
http://www.mydomain.com/folder/parameter/
have been unsuccessful.
I have tried the following:
RewriteRule ^folder/(.*)/(.*)%(.*) /folder/$1/ [NE,R=301,L]
RewriteRule ^folder/(.*)/(.*)3C/?php /folder/$1/ [R=301,L]
RewriteRule ^folder/(.*)/(.*)writelink /folder/$1/ [R=301,L]
RewriteRule ^folder/(.*)/([^/.]+)writelink /folder/$1/ [R=301,L]
But all of them are returning the same 403.
I have the test rewriterule as the first rewriterule in the file, so it isnt being usurped by something else.
(For reference, the correct rewriterule when I havent mucked up the page is
RewriteRule ^folder/(.*)/$ /content/element.php?param=$1 [L]
)
I've had problems with %ages in the path before but this time I've decided to defeat it - any suggestions?
Your URL is something like this:
http://www.mydomain.com/folder/parameter/</?php echo writelink(); ?> whithout the encoding.
The 304 code does not really indicate an error, it indicates the resource for the requested URL has not changed since last accessed or cached. Clear your brower's cache and make sure it is cleared.
The error should be 403 (Forbidden) because of the initial character < (%3C).
These errors make any rewrite rule at .htaccess useless. One way to handle this kind of problem is with a script.
EXAMPLE
Add these lines to your .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
ErrorDocument 403 /Error403.php
Create Error403.php at root directory with a content similar to this one:
<?php
// The following lines should be at the top of the file
/**************Only for Debugging**********************/
echo $_SERVER[ 'REDIRECT_QUERY_STRING' ] . "<br /><br />";
echo var_dump($_REQUEST) . "<br /><br />";
/*=====================================================
NOTE: A Header error might be generated while the above
code is active. Use it only to display the incoming
parameters and delete it for normal operation.
*******************************************************/
if ( isset ( $_SERVER[ 'REDIRECT_QUERY_STRING' ] ) ) {
$QueryString = $_SERVER[ 'REDIRECT_QUERY_STRING' ]; // The query looks like this: php%20echo%20writelink();%20?%3E
// Check if it is the wrong URL
if ( preg_match( '|php%20echo%20writelink()|i', $QueryString ) ) {
header("Location: http://www.mydomain.com/folder/parameter/");
}
}
// Handle other errors
?>
In this specific case we take advantage of the fact that the string contains a question mark ?, that makes it look like a query. So we try to match the query content with preg_match().
That should do it. Modify the links accordingly if necessary, this is just an example on how to do it.
I have a folder named /test in my application.
Right now i am trying to write an .htaccess file that would show all requests to /test* as /test.
For example:
www.example.com/test/ is the actual directory with index.php file in it.
All the requests like the following should go to the same /test directory
www.example.com/test-hello/
www.example.com/test-world/
www.example.com/test-htacess/
www.example.com/test123/
Basically any requests to /test* should go to /test.
This is what I've tried so far:
RewriteRule ^/test* /test
You need to use RewriteCond to first match "test in url"
Try below:
RewriteCond %{THE_REQUEST} ^GET\ /test/
RewriteRule ^test/(.*) /test/$1 [L,R=301]
Your regular expression is wrong. You mean ^/test.*$. Your rule would match to /testtttt.
The asterisk means that the char in front of it can be zero or more times included. The dot is a special char which means here could be anything. the .* matches every string including an empty string. See also Wikipedia.
You currently are not putting the -hello, -world etc behind your folder. What is hello? Is that the file? Or the param?
The second part of the rewriteRule should be a file. Something like
RewriteRule ^/test(.*)$ /test/$1.php
Above function will have:
/testABC to /test/ABC.php
But I don't understand what you want to accomplish?