.htaccess RewriteRule directory - .htaccess

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>

Related

HTACCESS short url and modify my url

How can I use .htaccess to make this URL
http://domain.com/index.php/sub1/sub2/333
look like
http://domain.com/333
?
Thank you very much!
I tried everything and I tried to find everywhere the answer, but nothing works.
This is my htacces, maybe can help you ( I hosted in hostgator,in hostgator is importan write your user, if is local remove ) this configuratio apply to multiple sites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<myuser>/%{HTTP_HOST}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /<myuser>/%{HTTP_HOST}/index.php/$1 [L]
</IfModule>
You can replace %{HTTP_HOST} by your folder.
After if you want modify the controller name, you need modify in application/config/routes.php
Following rule should work for you:
RewriteEngine On
RewriteRule ^([0-9]+)/?$ /index.php/sub1/sub2/$1 [L]

.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.

.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.

Having much trouble with mod_rewrite

I'm having some problems in my .htaccess file.
I would like to display the content of this URL:
http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456
When I access this one:
http://mywebsite.com/admin/payments/invoices/view/123456/
Here's my actual .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/
RewriteRule ^/view/([0-9]+)/(index.php)?$ /view/index.php?id=$1 [L,QSA]
Do you have any idea?
Thanks!
If you do have the view directory, then the easiest thing is to put this .htaccess in that directory (i.e. {DOCUMENT_ROOT}/admin/payments/invoices/view/.htaccess):
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/view
RewriteRule ^(\d+)$ index.php?id=$1 [L,QSA]
The index.php in the left side hand of the RewriteRule is not required (actually, I expect it not to work: the DirectoryIndex should not yet have been injected in the URI at this stage - unless some other RewriteRule is in effect?), nor is the / at the end of the RewriteBase.
I tested the above on Apache/2.2.21, but the module rules are the same for later versions.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*admin/payments/invoices/view/index\.php\?id=([0-9]+) admin/payments/invoices/view/$1/index.php [L]
</IfModule>
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)
RewriteRule ^admin/payments/invoices/view/index\.php$ /admin/payments/invoices/view/%1/index.php [L]
So when you enter http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456 in your browser's URL address bar, you will get served the content at /admin/payments/invoices/view/123456/index.php. Since it's completely unclear what you actually want, in case you wanted it the other way around:
RewriteEngine On
RewriteRule ^admin/payments/invoices/view/([0-9]+)/(index.php)?$ /admin/payments/invoices/view/index.php?id=$1 [L,QSA]

Why $_POST is empty when using [QSA] in .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!

Resources