Custom URL rewriting not working in codeigniter - .htaccess

I use the code below in a Codeigniter htaccess file
RewriteRule ^([^.]+)-vacation$ /search?location=$1
When i access it with the url www.example.com/moscow-vacation,
it gives me a 404 error.
I have used the above htaccess code in a core php application. It works fine there.

Most likely, you don't have a search script. If the script is named search.php, you can use this slightly modified rule
RewriteRule ^([^.]+)-vacation$ /search.php?location=$1
If you have a controller named Search with a method location, you can either rewrite directly to
RewriteRule ^([^.]+)-vacation$ index.php/search/location/$1
or try
RewriteRule ^([^.]+)-vacation$ search/location/$1

Related

Rewrite rules for localhost AND live environment

I want to add rewriterules that works in local environment (localhost) and on my liveserver.
Why?
I don't want to change rules when I test my project locally and upload it to the liveserver.
Adding Rules
Here an example
(ANY-URL)index.php?page=somepage
change to
(ANY-URL)/somepage
So I used a rewritemod generator and pasted this into it:
index.php?page=somepage
The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)
RewriteRule ^([^/]*)$ /?page=$1 [L]
When I try to get to (http:)
//localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.
Testing
Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works.
Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php
Possible solutions?
Maybe it has something todo with that "RewriteBase"?
Do i have to set a basepath?
My .htacces is located here:
//localhost/myproject/development/.htaccess
Later I also want to change paths that look like this:
(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool
Also here a I'm looking for a solution that works on both environments.
Since the htaccess is located inside a sub-directory, use RewriteBase:
RewriteEngine On
RewriteBase /myproject/development/
Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.
<base href="/myproject/development/" />
Now, the rewrites in htaccess would be:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]

How to change the project name in CakePhp 3.x

I have this project called 'site01'.
It is published as: 'www.example.com/site01'. The pages are like this 'www.example.com/site01/page/1'.
I'm able to route the url to 'www.example.com/site01/1', but can I change the url to 'www.example.com/s/1' through the routes configuration?
Initially I think that you have to use .htaccess way, because the issue is related to the outside of the CakePHP folder.
You can create a new file named .htaccess in the upper folder and write the instruction witch redirect /site01/ to /s/.
sample code for rewrite url:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^site01.*$ /s/ [R=301,L]

.htaccess rewrite rules not working

I am using scssphp for my css preprocessing and right now my styles look like src="style.php/style.scss". What I was wanting is to use a htaccess to just write in the style and then anything ending in .scss would get run through style.php. So I tried putting this in my home directory.
RewriteEngine On
RewriteRule ^(.*)\.scss$ style.php/$1.scss
I even tried
#RewriteEngine On
#RewriteRule ^(.*)\.scss$ style.php/style.scss
Neither work, something is happening, because my style.scss is loading with a 500 internal server error, but I'm not sure where the error is.
Your rules are looping, try adding an additional check to not rewrite when the URI has style.php in it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !style\.php
RewriteRule ^(.*)\.scss$ style.php/$1.scss
The rewrite engine will continue to loop through your rules until the URI stops chanmging. What's happening with your rule is that a request like /path/style.scss is getting procfessed and rewritten to /style.php/path/style.scss, and the the rewrite engine loops. The second time around, the rule gets applied again and it rewrites to: /style.php/style.php/path/style.scss, etc.
a better regex to get requested file name would be [a-z_A-Z]+.scss
try your htaccess using this syntax

mod_rewrite to not visible file

I'm missing something about .htaccess.
working tree like:
/
/subdomain
/subdomain/.htaccess
/subdomain/index.php
/assets
/assets/...
the htaccess is inside subdomain and I'm accessing this like: http://subdomain.localhost/
assets is outside of subdomain and I did this rule:
RewriteRule ^assets/(.*)$ http://localhost/assets/$1
Now, when subdomain/index.php tries to load something like assets/style.css the rewriterule redirect to http://localhost/assets/style.css.
The problem appears when I try to load a javascript file. It's recognized as a different domain, and doesn't work properly.
Is there a way to make http://subdomain.localhost/assets works as (and not a redirection) http://localhost/assets via some magic rewriterule or something else?
I tried ^assets/(.*)$ ../assets/$1 but it seems not working.

url rewriting asp .htaccess file

I just moved my website (asp.net) to the live environment. I realized they are running IIS 6 so all my nice and clean url rewriting doesn't work anymore. I was trying to implement URL rewriting using the .htaccess file.
I want to rewrite:
www.amicobio.co.uk/Menu.aspx to www.amicobio.co.uk/Food-Menu
So in .htaccess I set:
CaseInsensitive On
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^Food-Menu$ Menu.aspx
But it doesn't work it says:
The requested URL
/a/m/amicobio.co.uk/public/Menu.aspx
was not found on this server.
Obviously the path is wrong but what is /a/m/ and how do I fix it? All the files in amicobio.co.uk/public/
Thanks.
I found the solution
it was
RewriteRule ^Food-Menu /Menu.aspx
in stead of
RewriteRule ^Food-Menu$ Menu.aspx

Resources