I have a PHP page reading $_GET variables in as category and page.
I then have the .htaccess forwarding url.com/categroy/page to the index.php file for it to do the work.
I cannot however get the htaccess rules working
RewriteEngine On
RewriteRule ^/([^/]+)/(.[^/]) /index.php?category=$1&page=$2
RewriteRule ^/([^/]) /index.php?page=$1
Any ideas as to what i am missing?
Your rules lack the endling delimiter $. And the . is misplaced at that position. Also you need a + quantifier for the second [] character class:
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
I'm not sure about this, but I also removed the leading / slash. It should be implicit when you place your .htaccess in the root folder.
Additionally you probably will need the typical RewriteCond to exclude any real filenames from getting rewritten. Place this before each RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
Related
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
Basically I want to rewrite my urls so that it is website.com/folder/ sometimes though I need it to rewrite also website.com/folder/page/
Currently I have it working with just the website.com/folder/ but can not get it to check if there is a page, if I create just another rule under the folder one it reads that one, and gives me an empty page var, which is breaking my php. I struggle with .htaccess and any help would be appreciated.
Here is what I have that works with just the folder but I can not include a page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|docs)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
Here is what I tried to get it to work with either just a folder, or a folder and page
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)/$ /?folder=$1&page=$2 [L,QSA]
Please Help!
Accordingly to the RewriteRule docs you should reverse the rules order in your rules set. Because in your configuration both rules have the same RewriteCond, the most specific rule (folder + page) should be atop and the most general rule should be the last one. If not when the first rule is matched the URL is rewritten and the second rule never matches. Also, probably you want to remove the trailing forward slash in the pattern of your folder + page rule (assuming that the second group in the pattern matches a page not a folder). So I think the whole thing should read:
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)$ /?folder=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [L, QSA]
I'm working with mod rewrite, but my code doesn't work. It worked for a time.
I have stripped the code.
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/$ index.php?test=$1
PHP
<?php
var_dump($_GET['test']);
?>
If I go to the index.php it displays NULL.
I don't understand why it doesn't work anymore. I hope you can help me.
P.s. I have tested whether the .htaccess file is loaded by making a login form with .htaccess.
You need to get rid of the first slash:
RewriteRule ^(.*)/$ index.php?test=$1
And even then your rule will only apply when you enter a url that ends with a forward slash, so for example:
/index.php/
If you want it to work with any url, you need to remove the last slash as well:
RewriteRule ^(.*)$ index.php?test=$1
Edit: To avoid rewriting of existing files and directories you need add some conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?test=$1
You need to remove the first slash in your rule
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ index.php?test=$1
I'm developing a php application and I have a little issue with Apache and Mod Rewrite. Anyone knows what's wrong here?:
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule !^img\/.* index.php
When I put http://localhost/css/css.css appears index.php, maybe I'm missing something...
Why when the url matchs with the first rule apache doesn't stop the rewriting process?
'last|L' (last rule)
Stop the rewriting process here and
don't apply any more rewriting rules.
This corresponds to the Perl last
command or the break command from the
C language. Use this flag to prevent
the currently rewritten URL from being
rewritten further by following rules.
For example, use it to rewrite the
root-path URL ('/') to a real one,
e.g., '/e/www/'.
I have readed forums and docs since 3 hours and I still have the same problem.
Thanks in advance.
Centauro12, the problem is, that the [L] flag in fact stops propagation through the following rules, but then (if you are in an .htaccess file) the URL mapping starts over again. That means, all your rules will then be processed a second time. See the Apache Rewrite Guide for the details.
Therefore you need to explicitly disable rewriting for your rewritten php scripts:
RewriteRule ^css/css.php - [L]
RewriteRule ^js/js.php - [L]
or more compact (although perhaps not what you want):
# don't rewrite anything that really exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
I've found a solution:
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^css\/(.*)$ css/$1 [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule ^js\/(.*)$ js/$1 [L]
RewriteRule ^img/(.*)$ img/$1 [L]
RewriteRule ^(.*)$ index.php?rewrite=$1
It works fine, but I don't know why it's necessary
RewriteRule ^css\/(.*)$ css/$1 [L]
and
RewriteRule ^js\/(.*)$ js/$1 [L]
I hope it hepls anyone.
Thanks! :)
try
RewriteRule ^/css/css\.css css/css.php [L]
RewriteRule ^/js/js\.js js/js.php [L]
RewriteRule ! /^img/.* index.php
ie. if you ^-anchor the pattern to the beginning of the string, start it with a /. patterns are matched against URL-paths, which start with /.
EDIT
above is valid for server config, virtual host, and directory context only. if the context is .htaccess, the per-directory prefix including the first slash is stripped before the rule is matched (and prepended afterwards), so no need for ^/ here.
You might have to set your rewrite base to "/" before starting your expression with "^css..."
RewriteBase /
I have a problem with the regular expression at the bottom. I'm trying to pass all patterns except index.php, robots.txt and any css and js file through the index file, however I have one specific path where I need to process js and css files through php. Normally my url structure is like this:
example.com/class/function
lets say I want to process css and js files from the below pattern:
example.com/view/file/xxxxx.js/css
normally I would have thought this code would work but it doesn't:
(.*)(?!view\/file)(.*)\.(js|css)
for some odd reasons this code work (removed 'view' and added / before (.*)):
(\w*)(?!\/file\/)\/(.*)\.(js|css)
but then I won't be able to load files from the main directory:
example.com/file.js
And when I modify the file like this, it doesn't work again... (all I did was to make / optional):
(\w*)(?!\/file\/)(\/?)(.*)\.(js|css)
Here is the original htaccess file:
RewriteCond $1 !^(index\.php|robots\.txt|(.*)\.(js|css))
RewriteRule ^(.*)$ /index.php/$1 [L]
Assuming I have understood correctly, something like this should work:
#rewrite specific css/js
RewriteRule ^view/file/(.*)\.(css|js)$ file.php?file=$1.$2 [L]
#only rewrite anything else if the path is not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite to main handler
RewriteRule ^(.*)$ index.php/$1 [L]
For readability and debuggability, I recommend you separate some of your rules.
# Rewrite the special case
RewriteRule ^view/file/(rest of rule)
# Skip these
RewriteRule ^index\.php$ - [L]
RewriteRule ^robots\.txt$ - [L]
RewriteRule ^(.*)\.(js|css)$ - [L]
# Main rule
RewriteRule ^(.*)$ /index.php/$1 [L]