I am trying to use htaccess file to rewrite urls, I really have zero experience at this and everything I have done is from searching here and on google. My links have query parameters in them. I send everything to an index.php page.
I have tested all the links before trying to rewrite them and they all work in the index page. So then I created an htaccess file to rewrite the links but all I get is a blank page, none of the variables are being sent to the index page. I use $_GET in the index.php page to retrieve the variables.
Here is a list of type of links I am trying to rewrite along with a copy of my actual htaccess file, also mod rewrte is on and functioning properly in apache. Please help me change my htaccess file to do EXACTLY what I am wanting according to the sample links I have shown below, please?
The directory rewrite is fine and works accordingly, it is the links that don't.
Also, if you notice in links #1 and #2 the page parameter does not and should not appear in the rewritten links for these 2 links ONLY but a blank or empty string is still sent to my index.php.
example--
<?php
$page= $_GET['page'];
$user= $_GET['user'];
$act= $_GET['act'];
$section= $_GET['section'];
$search= $_GET['search']
$xx= $_GET['xx'];
if($page == ''){
//Do some code
}elseif($page == 'search'){
//Do some code
?>
Rewrite these type of links
note - "http://" is front of all of these links, stack overflow wouldn't let me add it and post.
LINK #1
localhost/notetag2/johnsmith/all/1
--REWRITE TO--
localhost/notetag2/index.php?page=&user=johnsmith&act=all§ion=1
LINK #2
localhost/notetag2/johnsmith/all
--REWRITE TO--
localhost/notetag2/index.php?page=&user=johnsmith&act=all
LINK #3
localhost/notetag2/search/johnsmith
--REWRITE TO--
localhost/notetag2/index.php?page=seach&search=johnsmith
LINK #4
localhost/notetag2/pictures/1
--REWRITE TO--
localhost/notetag2/index.php?page=pictures&xx=1
Rewrite directory
pages
--REWRITE TO--
web
HERE IS MY ACTUAL HTACCESS FILE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /notetag2/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3§ion=$4 [L,NC]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3 [L,NC]
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2 [L,NC]
RewriteRule ^(.*)/$ notetag2/index.php?page=$1 [L,NC]
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=picturesh&xx=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L,NC]
RewriteRule ^pages/(.*) web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /notetag2/index.php [L,QSA]
</IfModule>
You need to fix a couple of your regular expressions. Since your htaccess file is in your notetag2 directory, you don't need to match against the leading "notetag2".
But first, your rules are not in the right order. If you want to match /search/something, then you can't have a rule that matches anything, otherwise it'll match /search. So these rules need to be at the top:
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ index.php?page=picturesh&xx=$1 [L,NC]
You also don't need the notetag2/ in your rule's target since that's implied because the rules are in the notetag2 directory.
So after those 2 rules, you need to take each of your other rewrites starting from the largest to smallest:
localhost/notetag2/johnsmith/all/1
--REWRITE TO--
localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all§ion=1
You almost had that, just had some random (.*)/ in front of the regex, which will make it never match what you want. You want this instead:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2§ion=$3 [L,NC]
Then for the next one:
localhost/notetag2/johnsmith/all
--REWRITE TO--
localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2 [L,NC]
The "search" and "pictures" should have been taken care of by the first two rules above.
Whatever other rules you had at the bottom of your old htaccess file, they need to go below these rules.
So that leaves only:
pages
--REWRITE TO--
web
Since these don't start with notetag2, you need to put these rules in a different htaccess file, the one in your document root:
RewriteRule ^pages/(.*)$ /web/$1 [L]
Related
I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom
Well I'm havin trouble creating .htaccess, anyway I have categories in my menu for example test1,test2,test3 that are the names of a folders, and what I'm trying to do is to point the second parameter of the url to index.php, for example http://myapp/test1 I want to be able from the index.php to have $_GET['cat'] = test1 and so on..
Here's what I've tried so far and didn't work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/$ index.php?cat=$1 [NC,L]
Any help with this? Much appreciated.
Try removing the trailer / character from your rewrite rule. ^(.*)/$ matches any URL that ends in a / like http://myapp/test/. You are probably looking for ^(.*)$ or to strip any leading slash ^/(.*)$. Note though that if index.php is accessed directly via f.ex. http://myapp/index.php this would also match and rewrite to http://myapp/index.php?cat=index.php and any images wouldn't also be directly accesible. So you might want to add some RewriteCond that first checks if the requested URL is an existing file with RewriteCond %{REQUEST_FILENAME} !-f before the RedirectRule to rewrite the URL.
I'm working on a website that has been built sloppily.
The website is filled with regular links that are translated into the corresponding .php pages by the .htaccess page.
This is it:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
First of all, I would love some help regarding whether or not this page has everything it should. I've never messed with it before.
Secondly and my main issue, if, for example, I would write the address www.thewebsite.com/foobar.html, it would be translated into www.thewebsite.com/cat.php?cat=foobar by the .htaccess page, and it would give a database error (and reveal information about the database).
I've put a check into cat.php which checks if the category exists, but I can't redirect the user to the 404 error page. There's a page called 404.shtml in the website, but redirecting the user to it causes the .htaccess to just change it again to cat.php?cat=404.
Is the way they used the .htaccess page normal? Should I change this system?
And how are users sent to error pages? From what I understood the server should be doing it on its own?
I would love some clarification... There is some much about this subject I don't understand.
Update:
This is my new .htaccess page
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
Because the redirecting is in the code and the user cannot see it, I allowed myself to write the link in a non-clean way. I tried turning it into a clean URL but the following does not do anything:
RewriteRule ^error.php?err=(.*)$ Error$1.html
Can someone please help me understand why? I thought since error.php is a real page, I should put it before the conditional but it didn't work. BTW, I saw in an article about .htaccess that the page should start with Options +FollowSymLinks. It seems to me that everyone sort of has their own way of writing it. Is there a guide or something like that, which I can be sure is authentic and covers all the bases there is about .htaccess?
Thank you so much!!
Using rewrite rules to work around links to .html pages that don't exist is unusual in my experience, but it's really just a different take on "pretty" URLs, e.g. www.thewebsite.com/foobar/ gets routed to cat.php?cat=foobar on the backend.
Your 404 issue is different. You need to be able to display error pages.
One option here is to rewrite requests as long as they don't request an existing file. This is very common for serving up static content like images, CSS files, and the like. To do this, you can use the -d and -f options to RewriteCond, which apply when requesting a directory and file respectively:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
Now, requests to 404.shtml should go through, because you're requesting an existing file on the filesystem.
Note that the RewriteConds only apply to the single RewriteRule that immediately follows. For additional RewriteRules, also include additional RewriteConds.
Your regex is wrong anywhere. Literal dot needs to be escaped using otherwise it will match any character. Also it is better to use L and QSA flags to end each rule properly.
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]
Several days ago I had a question about removing index.php from the address bar, so the address of the page looks shorter and better. The shortest solution of this problem was (RewriteRule ^index.php / [L,R=301] in the .htaccess file). And it works!
Since I put that string into the .htaccess, some pages are redirected to the main page. I spent a lot of time to guess, why. As I understand, the answer is: with RewriteRule ^index.php / [L,R=301], $_POST parameters are not sent to the next page. $_GET parameters are OK.
Once I remove RewriteRule ^index.php / [L,R=301] from .htaccess, everything becomes fine as usual.
Why does it happen and how to fix that?
Thank you.
The [R] flag will incur a redirect. And user-agents issue a redirect as GET request. There is nothing that can be done if you really want to shorten URLs down to the / root path.
You could however block POST requests specifically from being rewritten/redirected:
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]
You could try using [L,R=307] instead. 307's must not change the request-method according to the spec, but I don't know how browser implemented 307.
But the root of the problem is the use of <form action="____/index.php" ...
Just leave the action empty to POST to the current url e.g.
I'm using something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(css|images|js)/
# don't rewrite existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
And its working for all requests, rewriting it via index.php file.
If you need to redirect 301 (which stands for Moved Permanently code) check out this question: Is it possible to redirect post data?
POST values will NEVER survive an external redirect (the R=301), it's a security liability, so browsers will never support that. Remove the R=301 and you will be fine. You just should alter all existing links to the page to the shorter/prettier one (<a>'s but also form actions etc.)
I had the same problems but my htacces was like this:
RewriteEngine on
RewriteRule .* index.php [NC]
Just change NC for L and everything works fine.
Final code:
RewriteEngine on
RewriteRule .* index.php [L]
In My case I used .htaccess.
Refer : PHP $_POST not working?
i.e
action="booking.php" to action="booking" worked for me
I have a rewirte rule that rewrites my portfolio urls from
portfolio.php?cat=cat-name&project=project-slug
to
/portfolio/cat/slug/
That all works fine and dandy, I'm wondering if It's possible to add a parameter to the end of the rewritten slug. For example
/portfolio/cat/slug&preview=true
I'm trying to make it so I can preview hidden projects by appending the preview=true to the end. It works if I go to
portfolio.php?cat=cat-name&project=project-name&preview=true
I'm just hoping I don't have to create a rule that has /true I've tied various ways to get the value or preview but thought someone here may have a simple solution
Edit Adding .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*$
#portfolio
RewriteRule ^portfolio/?$ portfolio.php [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/?$ portfolio.php?cat=$1 [L,NC]
RewriteRule ^portfolio/([a-z0-9_-]+)/page=([0-9]+)/? portfolio.php?cat=$1&page=$2&
Add QSA as an option to the back of the RewriteRule (in the [] brackets). E.g.
RewriteRule ^portfolio/([a-z0-9_-]+)/([a-z0-9_-]+)/?$ portfolio.php?cat=$1&project=$2 [L,NC,QSA]
Please look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite for such an example.
You may also include a hidden field with that appropriate value.