Why $_POST is empty when using [QSA] in .htaccess? - .htaccess

I've got the following in .htaccess:
RewriteCond %{REQUEST_URI} !app/index\.php$
RewriteRule ^(.*\.htm)$ index.php?url=$0 [QSA,L]
Everyting works great but result URLs have empty $_POST.
I'm 100% sure that it's mod_rewrite bug, not HTML or PHP because when I change [QSA,L] to [L] it works great. But I really need QSA.
It happens at about 30% hostings so this may be some Apache config option.

What about modifying your rule in this way. Does it change anything?
RewriteRule ^(.*)\.htm$ index.php?url=$0.htm [QSA,L]

by default,.. maybe apache not enable mod_rewrite, try type this on your console
sudo a2enmod rewrite
sample my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

I had exactly this issue. It works on one server then not the next. Annoying and time consuming to say the least!
After a lot of checking using this php:
foreach ($_REQUEST AS $key => $value) echo $value."<br>";
at the top of the index.php to check what values were being sent from the htaccess file, (and many other checks I will not go into!) I eventually found you need to specify "RewriteBase"
Adding RewriteBase / to the top of the htaccess file made it work on the all the servers I tried.
(also, remember to set to: RewriteBase /folder-where-file-is/
where "folder-where-file-is" is the location of the index.php file if in a sub-folder)
Hope it helps you - knowing this would certainly have helped me many many hours ago!

Related

.htaccess redirect not working with index.php of sub directory

I have a directory structure like root/user/confirm/index.php
I want to make redirect like
Redirect this url
http://www.site.com/user/confirm/ASd2sasda4ass
To this
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
I am trying this way
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
It redirects this url properly
http://www.site.com/user/confirm/index.php/ASd2sasda4ass
To
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
But not working for this url without index.php
http://www.site.com/user/confirm/ASd2sasda4ass
It shows 404 not found error
Please see and suggest any possible way to do this.
Thanks.
UPDATE
Complete codes
Options +FollowSymlinks
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
</IfModule>
Try this (without confirm.php in the url. You shouldn't need it anyway):
RewriteRule ^user/confirm/(.*)/? user_confirm.php?confirm=$1 [L]
I solved this issue by removing this rewrite rule from root .htaccess
RewriteRule ^/user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
And placing another .htaccess file in confirm directory to hide index.php and processing query parameter there with following codes.
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?confirm=$1 [L]
</IfModule>
And now its working without index.php in url. Hope this helps others too with same issue.

Codeigniter and .htaccess file to ignore index.php

So I'm having a weird problem with .htaccess file and codeigniter. I have a wamp server on my local machine and there it works as expected. I have folders like that wamp/www/my_app and in my_app folder i have .htaccess file like that:
ErrorDocument 404 /index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
And in mine config.php gile i have:
$config['base_url']='';
$config['index_page']='';
$config['uri_protocol']='AUTO';
So i tried uploading that project to debian server on link like apps.myserver.com/my_app. I put my app to folder var/www/my_app. But the trick is that it doesn't work.
I have tried changing uri_protocol to all 5 options but no change. I also tried putting .htaccess file to var/www instead of var/www/my_app but no change either.
I keep getting that stupid 404 Not Found error when i try to access controlers without index.php prefix.
Any ideas?
EDIT: I checked phpinfo on server and found that mod_rewrite is in loaded modules.
The mod_rewrite module can have a couple different extensions. Your Debian server might use mod_rewrite.so instead of mod_rewrite.c.
That said, if you know mod_rewrite is enabled, and your application isn't being widely distributed and needs to check for it, you can safely eliminate the if checks.
You mentioned commenting out everything except lines 3 and 7 -- you commented out the RewriteBase definition, which broke stuff.
Try something simple, and work from there:
RewriteEngine On
RewriteBase /my_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Also, one variation you can do if an .htaccess is being weird, is to change everything after index.php to a query string. Note the ? here:
RewriteRule ^(.*)$ index.php?/$1 [L]
You may or may not to change the uri_protocol config setting in CodeIgniter. My guess is you specifically won't, but it's info for others who are having .htaccess issues.

.htaccess help shortening a url

i have been working on setting up a url shortener with codeigniter i have everything working great and it works now with shortened urls like below.
http://example.com/shorten/i/znAS
ok what am looking to do is shorten it just to the i with my .htaccess access i am a complete newbie with .htaccess and have no idea how to do this is have been following this example here. http://briancray.com/posts/free-php-url-shortener-script/
which uses
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^shorten/(.*)$ shorten.php?longurl=$1 [L]
RewriteRule ^([0-9a-zA-Z]{1,6})$ redirect.php?url=$1 [L]
</IfModule>
Above is not really relevant for mine as its code igniter based. ok so shorten is my controller and i have a function within called i,
i have tried various combinations but nothing has worked for me yet below is my .htaccess.
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shorten/(.*)$ shorten/i/=$1 [L]
#RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^(.*)$ index.php?/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
whats there is currently breaking the website at the moment just looking and google at the mo but thought i would also add a post here as well thanks
With CI (or any framework for that matter), it is best to never touch the .htaccess file, unless you really need to. Take a look at the documentation with regard to working with the routes.php file in your application/config directory.
Try this route setting in the array:
'shorten/(.+)' => 'shorten/i/$1'
Please let me know if you have any problems with this.

.htaccess RewriteRule directory

RewriteRule ^(.*)/?$ ./view.php?id=$1
This works if say an id = "abc123", I can access www.site.com/abc123 perfectly fine.
But I want to be able to access with www.site.com/view/abc123
So I tried:
RewriteRule ^view/(.*)/?$ ./view.php?id=$1
But to no avail. When I try to access www.site.com/view/abc123 it seems as if it doesn't even grab the $_GET request to get the id.
What's wrong?
My entire .htaccess:
RewriteEngine On
RewriteBase /~user/sitetest/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^view/(.+?)/?$ view.php?id=$1
Edit:
Using the above .htaccess, when I access:
http://localhost/~user/sitetest/view/abc123
It doesn't load the $_GET[] from view.php. I even tried echoing out $_GET[] but it shows up as blank.
If I edit it to be RewriteRule ^(.+?)/?$ view.php?id=$1
And access http://localhost/~user/sitetest/abc123 it works fine.
If I edit it to be RewriteRule ^view-(.+?)/?$ view.php?id=$1
And access http://localhost/~user/sitetest/view-abc123 it works fine.
Does it have something to do with the slash??
Try something like this:
# this next line is really important
RewriteBase /
RewriteRule ^view/(.+?)/?$ view.php?id=$1
Using RewriteBase, you are specifically telling the server to use view.php in the server's root. If you don't specify the RewriteBase, the server will use the view.php in the requested directory (being /view/). Hope this helps :)
I think you are missing / at the start of the regular expression:
RewriteRule ^/view/(.*)$ /view.php?id=$1
Fixed it by setting RewriteBase to / and using /~user/... in my RewriteRule
(creds to Tom Knapen, posting here because of code-example).
So, OP's .htaccess would become:
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^view/(.+?)/?$ /~user/sitetest/view.php?id=$1
</IfModule>
<IfModule !rewrite_module>
ErrorDocument 404 /home/user/public_html/sitetest/view.php
#OR perhaps:
#ErrorDocument 404 /home/user/httpdocs/sitetest/view.php
</IfModule>

Htaccess URL rewrite question

I have a challenge using Apache.
In my .htaccess file I'd like to convert requests like this:
url/portfolio/filename.htm
to:
url?filename
Any takers? Thanks for your time
You have a couple of choices, depending on how you want the URL to appear to visitors (and search engines).
If you want the externally visible URL to remain url/portfolio/filename.htm, Alec's solution worked after I removed the two RewriteCond lines.
RewriteEngine On
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
If other parameters could be in the query string and you want to preserve those, add QSA to the options in brackets at the end of the rule.
If you want people outside to see url?filename instead, change the rule to:
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]
Same qualification about other query parameters applies.
If this still doesn't help, I suggest you turn on rewrite logging and look in that log for more clues. Post them here and someone will help. You might have to put this part in httpd.conf. My Apache didn't like it in .htaccess.
RewriteLog ...path...
RewriteLogLevel 3 # you'll regret anything higher
Something like this should work, although I have not tested it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
This should work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]
or if you want to visibly change it:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
The second one would look like example.com/?filename to the user, whereas the first one would look like example.com/portfolio/filename.htm.
Hope that helps.

Resources