I made my own MVC app. And I am using the following RewriteRule in an .htaccess:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
Then when i go to this url:
http://localhost/myapp/dahsboard/index
I get my variable $_GET['p']=dahsboard/index & it works.
But I use AutoComplete from jQueryUI with Ajax and it send a variable $_GET['term'] with the value of the input used by AutoComplete.
My URL is then:
http://localhost/myapp/dashboard/index?term='myvalue'
My .htaccess doesn’t resolve it and I don't know how to.
Easy. Change your RewriteRule from this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
To this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1 [L,QSA]
Which uses the QSA flag which will carry over the query string to the rewritten destination.
So when you go to this URL:
http://localhost/myapp/dahsboard/index?term='myvalue'
It will be passed on like this:
http://localhost/myapp/dahsboard/index.php?p=index&term=myvalue
And if I place the following in index.php:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>
The returned output is:
Array
(
[p] => index
[term] => 'myvalue'
)
Additionally, if you want to easily debug the results you can do so by using R flag in addition to the L & QSA like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1 [L,R,QSA]
And then run a curl -I to check the Apache headers which should tell you where it will send the URL to via the Location: header:
curl -I http://localhost:8888/myapp/dahsboard/index?term='myvalue'
HTTP/1.1 302 Found
Date: Tue, 17 Jun 2014 16:07:29 GMT
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10
Location: http://localhost:8888/Applications/MAMP/htdocs/myapp/dahsboard/index.php?p=index&term=myvalue
Content-Type: text/html; charset=iso-8859-1
Now yes, the R flag adds extra path info of /Applications/MAMP/htdocs/ to the URL I am not 100% clear on why that happens. But for basic debugging like this, you can read the headers nicely to see that the final destination of index.php?p=index&term=myvalue is being sent. And once you know that, remove the R flag when you put these rules into production.
FWIW, the http://localhost:8888/Applications/MAMP/ reflects my local MAMP setup in Mac OS X. So ignore that. Your local setup will most likely return 100% different info. But the basic concept is solid.
To combine new and old query strings, use the [QSA] flag.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1 [QSA]
Related
I'm trying to change the URL of my website to show only the ID but It seems to not work...
I don't know why.. other commands of RewriteRule work well..
Actually the .htaccess file looks like belove
RewriteEngine On
RewriteRule ^/?([0-9a-zA-Z-]+)/$ article.php?articleId=$1 [L]
I want that it works like this:
Old_URL(to modify):
mywebsite.it/article.php?articleId=15
I want something like this:mywebsite.it/article/15
But the URL remains the same actually: always display this: mywebsite.it/article.php?articleId=15
Thanks in advance to every help :)
An internal rewrite will never change the URL visible in the browser. You are probably looking for an external redirection. Or better the combination of both:
RewriteEngine On
# externally redirect old URL
RewriteCond %{QUERY_STRING} (?:^|&)articleId=(\d+)(?:&|$)
RewriteRule ^/?article/?$ /article/%1 [R=301,L]
# internally rewrite new URL
RewriteRule ^/?article/(\d+)/?$ /article.php?articleId=$1 [END]
Those rules are meant to be implemented on top level. Best in the actual http server's host configuration. If you do not have access to that then a distributed configuration file will work (".htaccess") when located in the host's DOCUMENT_ROOT, but support for that needs to be enabled.
It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are happy with how things work. That prevents caching issues on the client side.
Requests like unwanted .php files hitting my ROR production log
Example:
[2019-08-07T03:42:45.415176 #32608] INFO -- : Started GET "/java.php"
for 213.74.248.28 at 2019-08-07 03:42:45 -0400 F,
[2019-08-07T03:42:45.419677 #32608] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/java.php"):
[2019-08-07T03:42:46.006077 #32608] INFO -- : Started GET
"/_query.php" for 213.74.248.28 at 2019-08-07 03:42:46 -0400 F,
[2019-08-07T03:42:46.011142 #32608] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/_query.php"):
How can i block these *.php hits in apache? i found something in nginx.but how can i block in apache?
Also, i found a 'rack-attack' gem file to block. since I'm using ruby 2.2 the gem is not supporting.
How can i get rid of this?
Try something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} (\.aspx|\.asp|\.php|\.jsp|\.cgi|bitrix|wp\-content)$ [NC]
RewriteRule ^(.*)$ - [F,L]
</IfModule>
I use the following code in htaccess file located in /projects/testsite
AddType x-mapp-php5 .php
RewriteEngine On
RewriteBase /
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . projects/testsite/index.php [L,QSA]
#ErrorDocument 404 /404.php
when i am in http://www.mydomain.com/projects/testsite/admin/articles/1/edit
and i press Save which redirects the request to http://www.mydomain.com/projects/testsite/admin/articles/1/save
all post data are lost.
What i get if i try to debug is
POST: Array print_r: Array ( )
GET: Array print_r: Array ( )
What should i do to my .htaccess file to keep redirecting all requests to index.php but preserving all post data?
Thank you in advance!
P.S. my site works normally if i set it in a Windows Server with web.config rewrite rules.
UPDATE #1
From Firefox Live HTTP headers i see a significant issue: a 301 Moved Permanently header is captured only in Apache (this is not happening in IIS)
HTTP/1.1 301 Moved Permanently
Date: Sun, 06 Apr 2014 13:48:06 GMT
Server: Apache
Location: http://mydomain.gr/projects/testsite/admin/articles/6/save
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 270
Keep-Alive: timeout=3, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
UPDATE #2
It seems that there is some relation with this issue:
How to preserve POST data via ajax request after a .htaccess redirect?
where the asker finds out that someone was forcing a 301 redirect rule for every request on top of the .htaccess file.
Is it the hosting provider to blame?
This was quite baffling, hope i saved someone from the same headache:
The problem was located in Parallels Panel>Websites & Domains>mydomain.gr> Hosting Settings
Locate the select field:
Preferred domain, it was set to domain.tld but all my requests where to www.domain.tld so a 301 redirection (you lose all post data) was forced by parallels and not by my applications files.
Note that the label in Parallels warns that:
Regardless of the domain's URL that visitors specify in a browser (with the www prefix or without it), a page with the preferred domain's URL opens. The HTTP 301 code is used for such a redirection. The 'None' value means that no redirection is performed.
So my solution was to change Preferred domain to None.
I have a problem with my .htaccess file, my problem is I have site on localhost and path to it is:
localhost/site/sitename/html/login.php?ref=company
and after uploading to server would be:
www.site-name.com/login.php?ref=asdas
What I needd to do is rewrite ref to looks like this (in both cases):
path_to_site/company/login.php
where company is
$_GET['ref']
For ex. I have this url:
localhost/site/sitename/html/login.php?ref=cola
And I want have this:
localhost/site/sitename/html/cola/login.php
How to do it?
Try this if it works for you:
RewriteCond %{HTTP_HOST} ^(.*)/$login.php$
RewriteRule ^(.*)$ http://www.site-name.com$1 [R=301,L]
I hope I understood what you want correctly. Also check if you have everything enabled and configured on your server correctly.
Look # http://httpd.apache.org/docs/current/mod/mod_rewrite.html for more information if youre using Apache.
I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as follows
root/
index.php
login.php
page1.php
page2.php
page3.php
page4.php
page5.php
page6.php
page7.php
page8.php
subfolder/
index.php
validpage.php
images/
css/
The subfolder folder in the above structure is unique in that it is the only subfolder containing php files.All of the other files are in the root folder.
I have been through these questions Mod Rewrite and PHP and Mod-Rewrite or PHP router? and mod_rewrite, php and the .htaccess file
Updated htaccess. thanks to #thomasmalt
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^.*$ - [NC,L]
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
# add .php to the url
RewriteRule ^(.*)$ $1.php
Problem now is I get a 500 instead of a 404 error, when I try to access a non existent page like localhost/inex.php. The Apache error log gives me the following errors. (I accessed localhost/index which loaded the contents of index.php , followed by which I loaded localhost/inex which gave me a 500 error.)
[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Update: found the answer to my question here. The question is resolved.
Apache has an option: Options +MultiViews
If you enable this, you don't need to place the file-extention in the url.
I'm not totally sure what you're trying to achieve, and i suspect, no disrespect intended, that you need to understand both mod_rewrite, and the way the webserver and php parses and executes files. I'm not sure how to answer without shooting over or under your level of understanding, but I'll try.
I can tell you what you're current .htaccess is doing: (pseudocode)
if REQUEST_FILENAME does not exist then
rewrite request to /index.php/<the request>
Since index.php is your php script that is executed and everything put in $1 is ignored.
That's why index.php is executed whatever you do.
What you could try to put in your .htaccess is this:
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ $1.php
That should take the request and try to append .php to the end of it. I haven't tested it unfortunately, but you should give it a try. The result should be something like this:
if request is "/news/article"
translate it to "/news/article.php"
And you should read the mod_rewrite documentation twice a day for a month, and buy the O'Reilly book about Regular Expressions.
And if you have administrator access to the development server you should look into
RewriteLogLevel
RewriteLog
From experience, it's harder than it sounds to retro-fit mod_rewrite into an existing site.
This isn't because mod_rewrite is hard -- actually it's relatively easy to set it up and get your new URLs working. The reason it's hard is because your existing site will have all the old URLs hard-coded into it, so you'll have to go through your code changing them all to point to the new URL format - this can be especially tricky if your code builds up URL strings dynamically. You'll need to do lots of testing to make sure you've picked up every possible variation and that they all still work.
Also worth mentioning that search engines and external sites will have historic links in to your site using the old URLs, so you can't get rid of them completely.
I know that doesn't answer your question, but I believe these are important things to consider before you start the project, because for an existing site it isn't the quick-fix that some people make it out to be.
It send you to root because you have RewriteBase / what defining default base ...
If you want to stay in subfolder just make another .htaccess in subfolder with RewriteBase /subfolder/
or remove it
you can make condition like you want
RewriteRule ^(([0-9]+)-)?page(-([0-9]+))?/?$ index.php?page=$2&id=$4 [L]
this will take you from www.abcd.com/2-page-3 to www.abcd.com/index.php?page=2&id=3
Code in index.php doesn't need to contain anything.