I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....
Related
I'm aware of a lot of solutions around there, and tried everything. The problem is, when I put a .htaccess in the codeigniter subfolder, it's not read. I thinks it's because of the AllowOverride directive, but I can't change that.
I tried to skip the main subfolder that is not part of wordpress, one I called "sandbox", in the root htaccess (that's part of wordpress), so it doesn't "inherit" any problem from that rewrite (for example, to not have 404 managed by wordpress in the codeigniter project).
The problem is, as I said, that the .htaccess in the /sandbox/ciproject/ folder is not read. So, I tried this in the main .htaccess, and it worked... for a little time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?sandbox/
RewriteRule . /index.php [L]
RewriteBase /sandbox/ciproject/
RewriteCond $1 ^(application|system|private|logs)
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|img|fonts)
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
That allowed me to make it work for a while... but it's not working anymore. Something more I could try? I already changed the config of the codeigniter project so it doesn't use index.php, but nothing. I only get a server error 500 now, and if I delete the second half of the htaccess, I only get 404 as a response for getting rid of the index.php
I don't know if I'm losing some data, but just ask and I'll edit this, so you're not blind at this one.
Based on my understanding, your code-ignitor part doesn't work because, you have two clashing rewrite rules in your htaccess.
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
These are a clashing rule set. Remove one of the lines and then try. It should work.
Use only this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandbox/ciproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
I am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url.
this is what I have:
http://localhost/inbox.php?pg=2
I want
http://localhost/inbox/2
I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this
Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:
RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).
Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2
RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]
Explanation: How this work
Call to action: RewriteRule
Pattern: ^inbox/([^/.]+)/?$
Rewrite: /inbox.php?pg=$1
Command Flag: [L]
Simply,
RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1
Source: 10 Simple examples to rewrite using htaccess
I am currently trying to write a rewrite rule. I want to simplify the entered URL so that it will always use the index. The input url would be www.example.com/home.php and would be rewritten to www.example.com/index.php?r=page/home.
I wrote this .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?r=page/$1 [L]
This configuration unfortunately gives me an error 500. I can understand that it is caused by the fact that if I enter index.php for instance, apache will not know if it must use the index.php file or use the rewritten url index.php?r=page/index.
Is it possible to accomplish this kind of rewriting rule with apache? If so, how can I fix my error 500?
Edit: Please note that the RewriteRule works fine if I change the extension .php to anything else such as .html, as so: RewriteRule ^([^/]*)\.html$ /index.php?r=page/$1 [L]
You have two possibilities.
First one
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule ^([^/]+)\.php$ /index.php?r=page/$1 [L]
Second one
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /([^/]+)\.php
RewriteRule ^.*$ /index.php?r=page/$1 [L]
you can also try with this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=page/$1 [L]
Currently making a php template/framework. Now I have done as advised and put all normal files in a PUBLIC folder with libraries and config in others and have placed the index.php into the public folder but then try and do a MOD_WRITE and nothing works - Im using the Coral8 Server (For testing) and have configured it all correctly to do it but doesn't seem to be working.
Here's what I've tried:
RewriteEngine on
RewriteRule ^public/?$ /public/index.php
RewriteRule ^public/([^/]+)/?$ /public/index.html
and this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>`
and this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
But none seem to work :(
Thank you in advance but someone tell me what I'm doing wrong so that I can learn from the mistake and then how to correct it.
Thank you
Is this what you're looking for?
RewriteEngine on
RewriteRule (.*)\.html$ /public/?action=$1&%{QUERY_STRING}$ [L]
RewriteRule ^$ /public [L]
So your URL will look like relution.co.uk/about.html
RewriteEngine on
RewriteRule ^$ /public/index.html [L]
//made addition on index.html and put it first so that it is the first rule to be applied and then makes the condition for the next rule to apply
RewriteRule (.*)\.html$ /public/?action=$1&%{QUERY_STRING}$ [L]
I want to be able to handle mod rewrites from within PHP instead of my .htaccess file, this way when I have custom modules that need a new rewrite I don't have to redo the .htaccess file.
I want to mimic the way that WordPress does their .htaccess which is:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
My current .htaccess is this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]
Does anyone know how to make this happen?
My way is to replace
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
With
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
Then you have to do similar parsing and everything as modrewrite does, but you can do it yourself using preg_match on $_GET['path']. I.e.
if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
Do code;
}
Sure, have the .htaccess file look like the first example you have, and build your script from there.
Probably write some sort of Router class, to route the requests to their places, the fundamentals being splitting the received query by /, which gives you an array of URL parts, and start conditioning.