Total noob at mod_rewrite - php

I am trying to rewrite my URL and having serious issues. I am on a Godaddy linux hosting server and it didn't come with any type of config file or .htaccess file. I created my own .htaccess file with the below rewrite info but I have no idea how to do anything else:
RewriteEngine On
RewriteRule ^NSN/([^/]*)\.html$ /nsn.php?NSN=$1 [L]
The people at Godaddy told me I could use URL Redirect to help with this but I am not even sure what that means.
If someone could please help with the next steps of how to make this work, it would be greatly appreciated.
Thank You.

Rewriting URLs is mapping the URL you see in the browser from the default to something more meaningful or memorable. The simplest example is something like this:
# Replace all html references with php
RewriteRule html php
A testing tool will save time, and understanding how to use aliases or redirects to avoid regular expressions may be helpful.

Related

php file not accessible when url typed in to browser

I tried to search for this on the forums, but I don't know exactly what my issue is called. I hope this isn't redundant to another forum or a really stupid question. This is also my first post, so I read through what I need to put in this question, but please help me learn if I forgot to include something.
I FTPed a .php file over to my server into the /public_html folder which is where my index.php file are as well as everything else that I can get to show in my web browser (chrome). The file is named game-response-confirmation.php. I have the permissions for this file set to 777 on the server as well just for testing purposes (i'd like to set them back to 644 when I'm done if that doesn't have to do with the issue).
The issue is when I type in the url www.mywebaddress.com/game-response-confirmation.php into the address bar, I receive a 404 Not Found error. It says that the file was not found on this server.
To be honest, I'm not sure what other information you need to help troubleshoot this issue with me. Ask and I will provide more information.
For this, I would expect the page game-response-confirmation.php to appear as a webpage in the browser. Currently, the website is a simple echo since I needed to make sure it wasn't the php file that was creating the problem.
Thank you in advance for any help.
Look at ".htaccess" file. Perhaps there is wrong redirection inside.
For instance:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Third line obtains all URLs and sends them to unknown place. In result you get error #404.
Golka had a great suggestion. going into the .htaccess file gives some information about what is going on. It told me, indirectly, that my host was switching over the server. So I was able to investigate and figure out that they are in the middle of the migration. Because of this, I didn't have the new server information and was uploading the .php page to the old server while the domain was pointed to the new server. So of course this file wasn't available! Silly. Thank you all for suggestions and help.

Treat directory as request for a PHP file

This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.

CakePHP strange cache file

this is the issue:
CakePHP is generating strange cache files in cache / view
"2f400_shtml.php"
"d_allow_url_include_3don_d_auto_prepend_file_3d_2f_2f_2f_2f_2f_2f_2f_2f_2f_2f_2f_2fetc_2fpasswd"
do not quite understand what happens, but reviewing files I saw this in the request unserialize of this file:
"query";a:1:{s:10:"/400_shtml";s:0:"";}
someone already had this problem?
This occurs in the production server but not in my local environment.
can these attempted attacks?
Thanks, your comments are appreciated.
Yes it looks like someone is messing about with your site. Looks like they are trying to access the /etc/passwd file.
Someone has hit a URL that has made Cake create the cache file. Cake will URLencode the url that has been hit and replaces special characters with underscores
So the request would have involved a lot of / characters there.
Take a look at your apache access.log file and you will be able to see clearer the kind of request people have been making:
E.g.
cat <apache_logs_dir>/access.log | grep passwd
Will show similar requests to that second one there.
I would try it yourself to make sure they didn't have any success :)
Its probably time to ensure Apache only has access to the directories that you want it to. I think you can use the <directory> tag for this:
http://httpd.apache.org/docs/2.2/mod/core.html#directory
(I am only assuming you're using Apache, if youre using IIS you may need to investigate similar functions)
Looking around google it looks like it might be someone trying to exploit this vulnerability:
http://blog.sucuri.net/2012/05/php-cgi-vulnerability-exploited-in-the-wild.html
Extract from that link:
The PHP guys are recommending the following .htaccess hack to block those attacks:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^[^=]*$
RewriteCond %{QUERY_STRING} %2d|\- [NC]
RewriteRule .? – [F,L]

URL rewrite in apache server

I'm in a situation where my web page have the following types of URL's
http://mysite.com/?type=home
http://mysite.com/?type=aboutus
http://mysite.com/?type=contactus
For the end users I need it to display like the following:
http://mysite.com/home
http://mysite.com/aboutus
http://mysite.com/contactus
This means, the user is not aware that the URL have a request parameter "type" in it.
I'm not sure it is possible or not.
Please help me to get to a solution.
EDIT: I searched lots of websites to learn URL rewrite by using .htaccess, But didnt able to make them working in my server.
Place this in you .htaccess file
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ ?type=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ ?type=$1
Yes it is absolutly possible.
Your have to be sure your hostprovider has enable URL rewriting (I don't really remenber but I'm sur you can find help on Internet to verify that).
You have to create an ".htaccess" file at the root of your site.
Put this content in it :
tell the server to follow symbolic links :
Options +FollowSymlinks
activate the rewrite module :
RewriteEngine on
Rule for rewrite url
RewriteRule ^([a-z]+)$ /index.php?type=$1 [L]
Note this is just a qucik exemple you may want to look "url rewriting tutorial" on Google.

Please URL Rewrite in php

Please how can I rewrite
Could anybody please rewrite this url?
http://localhost/display_news_cat.php?news_cat_id=14&p=2
to
http://localhost/display_news_cat/14/2
Thank you
Create an .htaccess file in the site directory and add the following lines
RewriteEngine on
RewriteRule ^display_news_cat/([\d]+)/([\d]+)$ display_news_cat.php?news_cat=$1&p=$2
Afaik, this is normally accomplished with Apache .htaccess file rewrite rules.
Is you case this would look something like:
RewriteEngine on
RewriteRule ^display_news_cat/([0-9]+)/([0-9]+)$ display_news_cat.php?news_cat_id=$1&p=$2
If this doesn't work, try checking your access logs to see what's happening.
there are different ways to archive this, and it takes only 1 minute to find this out yourself using google. you could:
use an .htacces file with rewrite-rules to let the apache do the rewriting
map everything on localhost/ to an index.php, read and parse the request-string "by hand" hand show the correct site
Also you can hold it in one script use GET to retrieve the values you wish and re-create the URL with that values. I don't know if it will help you..
Anyway a .httacess file will be much more useful for you.

Categories