I have a switch on index.php, which listens to ?action=
I would like to rewrite anything after index.php/ to index.php?action=
Currently I have a rule to remove index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
So a current url looks like;
localhost:8888/?action=viewBasket
and would like to rewrite it to;
localhost:8888/viewBasket
Im getting confused as to wether I need to create a condition for each switch param, so to prevent images, script, sources etc from being rewritten. Or if there is a condition to check if exisits first. Cheers!
I have worked it out. Will remove my question tomorrow if no one provides a better soloution.
# redirect to any directories
RewriteRule ^styles/(.*) styles/$1 [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/$1 [L,QSA]
RewriteRule ^images/(.*) images/$1 [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/$1 [L,QSA]
RewriteRule ^scripts/(.*) scripts/$1 [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Try this out :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action=$1 [L]
First line: Added this because I was scratching my head why it was not working for me. I'm sure you know you need this, just a note for others who might view this.
Second line: Checks if it is not a file.
Third line: Checks if it is not a directory. If it's a file or a directory, it will not process your RewriteRules. By default RewriteConds are operated with and.
Fourth line: I'm sure somebody can write a better rule. [L] last rule, stop evaluating RewriteRules. Tells it to rewrite to index.php?action={whatever} without changing the location header.
The gist of the fourth line is this: if your index.php is
var_dump($_GET);
www.example.com/foo
Will yield array(1) { ["action"]=> string(3) "foo" }
Related
I have two requirements with the .htaccess file.
First one is to apply "/index.php" part for all urls as a prefix,so it wont need to add the same part manually for all the URLs in my codeignitor project.This part works fine.
The second requirement is I need to block direct access to my .PHP files and for that, I'm using another rewrite condition but its not working and I cant figure out what is wrong with my following code.
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} (.*)\.php
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
I'm very new to URL rewriting. What I'd like to achieve is to have 2 main rules:
Hide all .php extensions
Rewrite domain.com/p.php?id=1 to domain.com/p/1
Here's what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^p/(.+)$ p.php?id=$1 [L]
This seemed to work at first: all .php extensions are hidden, and typing domain.com/p/1 displayed domain.com/p.php?id=1.
However, I've just realized that the PHP GET method on this page picks up a wrong value: whereas I want it to pick up 1, it actually picks up 1.php/1. I didn't notice it at first because the database query based on $_GET actually works, which seems odd to me now that I know the value is wrong.
How could I make the combination of these two rewrite rules work as intended?
Thank you!
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+p\.php\?id=([0-9]+) [NC]
RewriteRule ^ /p/%1? [R=301,L]
RewriteRule ^p/([0-9]+)/?$ /p.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
If you just want to rewrite the numerical values, you should restrict your second rule:
RewriteRule ^p/(\d+)$ p.php?id=$1 [L]
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern
I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]