I am having some problems with my url rewriting in my .htaccess. They work well locally but for some reason i don't understand, they don't work on my server. I was able to make my first rule work by adding a question mark after the index.php like so (i also don't understand why it needs this question mark) :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index\.php?/$1 [L]
But i still cannot find a way to make this rule work on my server.
RewriteRule article/([a-zA-z0-9\-]+)-([0-9]+)/ article/$1/$2/ [L]
Could you tell me what am i doing wrong and why it works locally but not on my server ? Thanks !!
I'm guessing you need the question mark because your server isn't setup to handle PATH_INFO. But you need the article rule before your other rule:
RewriteRule article/([a-zA-z0-9\-]+)-([0-9]+)/ article/$1/$2/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index\.php?/$1 [L]
I finally managed work this out. I looked at the $_SERVER variable on my server and found out that the rewritten url was stored in the $_SERVER['QUERY_STRING']. Don't ask me why ...
So i am using this variable instead of $_SERVER['PATH_INFO'] and every things works perfectly again.
URLS like this : /article/lala-00000000164/ are invisibly rewritten as so : /article/lala/00000000164/.
Thanks for the help !!
Related
My greetings to all.
I have a problem for which I am looking for a solution.
I have an my-website.com site and every user who registers on my site gets a personal link: USERNAME.my-website.com .
Meanwhile, I had the following rule in my htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule (.*) process.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]
How to make all sub-domains ( USERNAME.my-website.com ) point to process.php ( process.php/USERNAME ) file and that the others urls like my-website.com/OTHER point to index.php/USERNAME ? ( in case OTHER is not a file or a folder of my site )
Here's what I did, but it doesn't work.
"But it doesn't work" is a bit vague, so all we can do is given some general advice and suggestions...
First you need to make sure that those subdomains are actually served at all by your http server. You did not write anything about that, nor how you host your site at all.
Also you obviously have to take care that distributed configuration files (".htaccess") are considered at all by your http server, also something which is not clear from your question.
Given both of those aspects you will indeed have to concentrate on your rewriting rules. Here is a variant of your own implementation with a few changes:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/process\.php$
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule ^ /process.php/%1 [QSA,END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index\.php/?
RewriteRule ^/?(.*)$ /index.php/$1 [QSA,END]
You probably will have to make some more changes. But based on the vague information you gave it is hard to be more specific.
A good way to start debugging issues with rewriting is to enable rewrite logging inside your http server. That allows to closely monitor what is actually happening inside your rewriting engine. Please consult the documentation about that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
i have the following rewrite condition and it works fine:
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
However, all it takes is adding a trailing backslash to my url and the site breaks. I have seen several similar questions and answers but nothing works for me, mainly because i'm not familiar with htaccess.
How can i make make my url auto change from
http://localhost:8888/mysite/mylink/
to
http://localhost:8888/mysite/mylink
Iam using codeigniter and my application is still in localhost. Appreciate any help in this. Thanks.
Just change all your links without that slash at the end.
Hi i've got another question
i have the simplest htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
it gives me friendly urls eg. www.mysite.loc/blog/
but i want to go further and have eg. www.mysite.loc/blog/?page=1 and www.mysite.loc/blog/?page=1&id=1
how can i do that ?
P.S. i've tried many solutions that i've found in google but nothing works
Maybe you want something like this:
RewriteRule ^(.*)$ /index.php?url=$1&%{query_string} [L]
This way, a request for /blog/?page=1 would get mapped to /index.php?url=/blog/&page=1 and /blog/abc/?page=3 would get mapped to /index.php?url=/blog/abc/&page=3.
You'd then parse $_GET['url'] to figure out what resource the browser asked for.
I've just learned a bit of CodeIgniter Framework and I saw something which I wondered a long time ago.
CodeIgniter handles Routing just as this:
http://mydomain.com/index.php/classname/function/parameter
Now I want to get this to work:
http://mydomain.com/classname/function/parameter
Is it somehow possible to get the webserver to not check for the directories but to forward those parameters to mydomain.com/index.php? Is this somehow realizable by .htaccess or something?
Thank you for help!
Here is the help regarding this http://codeigniter.com/user_guide/general/urls.html
or Place
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
in your .htaccess
The following .htaccess file works perfectly on my local server.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.cssc
RewriteRule . style.php [L]
RewriteRule ^admin\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . admin.php [L]
I am doing some work for a client and he is using 1and1.com. I do not know anything about his account or what package he has.
All files are rewritten to admin.php (unless they actually exist). The problem is, I am getting a 404.
I know it's reading the .htaccess file because:
I can put garbage in the file and get a 500 error.
If I do a general rewrite (all pages go to admin.php), it works.
Also, it seems that 1and1 does it's own rewriting. If I go to: http://somewhere/afile, it will include afile.js even though I am not requesting the .js.It is super strange.
Does anyone have any experience with this? Or any insight?
To add some information..
1and1 seem to have some sort of default rewriting already in place that can interfere with your own rules - for example:
RewriteRule ^product$ /product.php [L]
It gave a 404 for this, and also for any other rewrite rule that matched a pre-existing .php file in my root folder.
2 things that 'fixed' it:
Change rewrite text to no longer match filename:
RewriteRule ^productz$ /product.php [L]
Or, change filename:
RewriteRule ^product$ /product_.php [L]
Both work - both very annoying.
The answer was simply "Do not nest .htaccess files". I had one in a sub folder.