Rewrite URL with subdirectory and file - apache

i am trying to use .htaccess to rewrite the URLs for my site, here is what i would like
currently, the urls look like this
http://www.mysite.biz/store/cart.php?m=new_arrivals
All of the sites are loaded using php GET and the only thing that ever changes is the query string after cart.php so i would like to rewrite to....
http://www.mysite.biz/cart.php?m=new_arrivals
I have been searching all over the internet and have tried out various techniques to do this and none of them have worked. i've tried this...
RewriteRule ^store/(.*)$ http://www.mysite.biz/$1 [R=301,L]
this...
RewriteCond %{REQUEST_URI} ^/store($|/)
RewriteRule ^.*$ /store/cart.php [L]
and this...
RewriteCond $1 !^store
RewriteRule ^(.*) /store/$1 [L]
ive been developing web for a while now but this is my first time really doing anything with .htaccess and i am totally stumped. any help will be greatly appreciated

Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+store/(cart\.php[^\s]+) [NC]
RewriteRule ^ /%1 [R=302,L]
RewriteRule ^(cart\.php)$ /store/$1 [L,NC]

Related

htaccess is not working after changing server with codeigniter

i am using codeigniter with .htaccess
in older server i had following rules in .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
it was working fine.
but after moving the website to a new server the URL .htaccess wasn't working
it was showing "Input file is not specified"
after searching the web i found a solution that is:
changing the Rule in last line from:
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
to (adding extra ? just after index.php):
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
this solved generic URL rewrite with codeigniter.
but still i have problem with following part those are specific for separate URL:
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
in the first URL there are maximum 8 parameters (but the number of parameter can vary in different case
and in the second URL there is no parameter.
but these URLs are not working in new server.
now can anyone please help to make those two Rule working.
Not sure if it'll work, but did you try a similar solution as the one for index.php? Something like:
RewriteRule ^transaction\.php$ index.php?/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php?/printer_api/getlist [L]

htaccess is redirecting seems to be ignoring not rewritecond

I have the following scenario and I've spent hours trying to work out why it's redirecting.
Here's my htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/mypage/?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^mypage/?$ /testing.php [NC,L]
When I browse to http://www.example.com/mypage it is redirecting to https://www.example.com/mypage
I would expected because of the !^/mypage/?$ condition it wouldn't, however, I seem to be completely wrong!
Can someone please put me out of my misery and let me know why this isn't working?
Thanks in advance.
It is because 2nd rule is rewriting /mypage to /testing.php and your REQUEST_URI becomes /testing.php in the 2nd loop of mod_rewrite.
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(mypage/?|testing\.php)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^mypage/?$ /testing.php [NC,L]
Also make sure to test it in a new browser to avoid 301 caching issues.

Rewrite urls friendly with htaccess. how I can use it on my site?

I'm rewriting urls on htaccess using rewrite rules, but when I want that my site uses it I get stuck.
I want my links looks like:
mysite.com/section/this-is-the-title-of-this-section
I used rewrite rules to get this:
mysite.com/section/?id=longalfanum to mysite.com/section/longalfanum
that was cool for a moment. but I checked some few websites for the URLs I realize that they have friendly URL from the beginning
so I changed the url on the to looks like:
mysite.com/section/longalfanum-the-title-of-the-section
now my links doesn't work like before.
my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#this is for avoid extra /
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*/mysite.com/%{REQUEST_URI}? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#this is my rule
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
obviosly this doesn't work. am I missing a step?
Change
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
to
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
so it matches anything (.*) after 'section/', not just digits (\d+)
Also, you should add an [L] flag to all RewriteRules that don't currently have it to make things a little bit more efficient.
my html with php looks like this:
in the web browser looks like:
mysite.com/section/?idN=alfanum
my rule looks like:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
with this I only get URL like mysite/section/nosensealfanum
it is possible to get the title of new by the htaccess? or I its need to make changes in the way I get the new from the db?
thanks

Apache URL rewrite and redirect

I want to rewrite my url to a new path. From:
www.example.com/test.php?name=xxxx&id=xxxx
To:
www.example.com/test-namevalue-idvalue
When I typed www.example.com/test.php?name=xxxx&id=xxxx in browser, it will take a while to redirect to page www.example.com/test-namevalue-idvalue, but browser complains the website is in redirecting loop. So, I am wondering someone has also met this kind of issue before?
Here is the content of my .htaccess file for Apache:
RewriteEngine On
RewriteCond %{REQUEST_URI} test.php$
RewriteCond %{QUERY_STRING} ^name=(.*)&id=([0-9]+)$
RewriteRule ^test\.php$ /test-%1-%2? [R=302,L]
RewriteRule ^test-(.*)-([0-9]+)$ test.php?name=$1&id=$2 [L]
Try matching against the actual request instead:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+test\.php\?name=([^&]+)&id=([0-9]+)
RewriteRule ^ /test-%1-%2? [R=302,L]
RewriteRule ^test-(.*)-([0-9]+)$ test.php?name=$1&id=$2 [L]
I vaguely remember that I would intuitively set the flags of the last RewriteRule to [PT,L] instead of just [L]. Maybe you try that.

godaddy .htaccess RewriteRule

I'm trying to set up a .htacess file on Godaddy webhosting ( apache-linux ofcourse, not IIS ). But i`m stuck with a problem:
RewriteEngine On
Options +FollowSymLinks
Rewriterule ^templates/.*$ - [PT]
Rewriterule ^controllers/.*$ - [PT]
Rewriterule ^.*$ index.php [NC,L]
I`m getting an Internal Server Error for line:
Rewriterule ^.*$ index.php [NC,L]
I don't know how to fix this, i've tried everything I know... basically I want to send anything that comes to index.php where a bootstrapper is set. This is working on any hosting i`ve ever tried, but godaddy seems to have problems with this: ^.*$ any help would be appreciated.
This rule
Rewriterule ^.*$ index.php [NC,L]
looks OK, but make sure that there are no spaces between the flags
i.e [NC, L] should be changed to [NC,L].
You can try the equivalent to see if it makes a difference (you don't need NC because it already matches any request)
Rewriterule .* index.php [L]
If that is not the issue, then it is likely the rules before that cause it to fail. Comment them both out and see if it works, but add a RewriteCond as below
Options +FollowSymLinks
RewriteEngine On
#if its not already index.php
RewriteCond %{REQUEST_URI} !index\.php$ [NC]
Rewriterule .* index.php [L]
If those rules are the real cause, then I would like to know what was your intent with those rules, and can they be expressed a different way i.e. if the intent was to rewrite all content except that in the templates or controllers directory, then that could be achieved by
#if request is not for templates or controllers directory
RewriteCond %{REQUEST_URI} !^/(templates|controllers)/ [NC]
Rewriterule .* index.php [L]