How do i setup htaccess to allow changes to occur? - php

below is the rewrite rule:
RewriteEngine On
RewriteRule ^search/$ search/searchPage.php [L,NC]
the .htacess file is located at the root of the website
problem is that it does not exactly take me to searchPage.php when I just type domain.com/search/, it just takes me to a 403 error page (note there is no index.php page here).
This is uploaded in amazon elastic beanstalk

you are basically thinking of it backwards, what you need to do is call the full url and rewrite it to the real one.
like this,
RewriteRule ^search/category/(.+)/ domain.com/search/searchPage.php?crs_category=$1 [L]
and then you would use this as your url http://domain.com/search/category/business/ and rewrite ( not redirect ) it to where it is mapped. The url in the browser would stay the same.
I am not sure 100% of the .htaccess off the top of my head but that is the method you would use.
Make sense?
The (.+) bit is a capture group and would capture the category and the $1 is where that is output as it is the first capture group. Some further reading that would help is how to use regular expressions.
As I mentioned a router in the comments, the issue with this method is you have to map each one by hand, which is ok for one or two but can get real ugly real fast.

Related

GET request without needing the ? at the end

I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?
For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones
Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.
But how can I change that link above to:
http://www.example.com/user/JimmyJones
Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?
EDIT:
Here are some websites that do it just about how I would like to do it:
imgur.com/user/example
facebook.com/exampleuser
you make .htaccess file in the root dictionary then start it with
RewriteEngine on
then write your rules, For your example it would be like this
RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]
so a full page would be like this
RewriteEngine on
RewriteRule ^/?user/([^/]+)/?$ user.php?user=$1
just for your example.
In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:
RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]
The first line says use the rewrite engine.
The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).
The L flag basically says job done.
If the .htaccess were in the public root:
//example.com/user/JimmieJones
would map to:
//example.com/user.php?user=JimmieJones
However it will not match:
//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9
Note that any existing links in your application:
Visit Jimmie's Profile
Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.
Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.
I reserve first level patterns for aliases/shortcuts/campaigns.
//example.com/slug
I'd avoid that for your user profile urls if possible.
You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).

Using a backreference at the beginning of a pattern in an apache rewrite rule

I'm currently translating a website from english to spanish. I want to use a rewrite rule that can put the current language at the beginning of the url, just after the domain. Example:
current url without rewrite rules:
http://www.example.com/sample_subdirectory/?language=spanish
objective:
http://www.example.com/spanish/sample_subdirectory/
At the time of this writing, I have all my php files rewritten as if they were subdirectories (such as example.com/something.php to example.com/something/) so there's no problem there. My objective though is to see if there's a way to rewrite the url so that instead of displaying the language GET variable, it displays "/spanish/something/something_else/etc/"; The goal is to not have to do an individual rewrite rule for every url just for the spanish translations, but rather append that /spanish/ subdirectory towards the beginning of the url. I tried to figure out how to use the metacharacters but I don't think I was doing it right. Here's what I tried (I'm pretty sure this is done so wrong :/ )
RewriteCond %{QUERY_STRING} ^id=(spanish)$
RewriteRule ^/spanish/(.*)$ /$1?id=%1
I'm still kinda new at rewrite rules, I only know basic stuff, I'm definitely not a web server administrator so I wouldn't know how to implement what I'm going for. Hopefully it's possible to do this. Ironically, I could have probably finished typing simple rewrite rules instead of spending an hour trying to do this but oh well :P
Based on the example you give above, the following should do what you want:
RewriteCond %{QUERY_STRING} ^language=spanish$
RewriteRule ^/(.*)$ /spanish/%1?
The RewriteCond causes the RewriteRule to be run only when the query string is "language=spanish".
The RewriteRule saves the directory from the original URL and appends it to the directory "/spanish/". The ? at the end removes the query string.
Thanks to bobdye's insight, I realized I had to remove the condition altogether and tweak the RewriteRule to look like this:
RewriteRule ^/subdirectory/spanish/(.*)$ /subdirectory/$1?language=spanish
Note: /subdirectory is a folder inside a virtual server (localhost) that I have running on my laptop.
As you can see, I had to place the /spanish/ directory in the pattern and whatever subdirectory was under that I would use as a back reference to the actual link, then I would just append the GET variable language=spanish to the end of the link. I'll still need to test to make sure that this works for any number of subheadings under /spanish/ but this definitely solved my problem.

How to make multiple product or news sites without making tons of PHP files

i was thinking, regular site have just few subpages like:
-home
-about
-contact
And things like that, so in general you usually get below 10 .php files, but what if somebdy want to make CMS which generate new site for each news, right it is possible to make new php file for each of it but its unproductive.
I also find a way that i can just make something like "yourdomain.com/news/index.php?id=24" and just get data from mysql where ID = 24 but i need to display name of news in URL
So my question is how to make something like speciic URL for each news for example: www.yourdomain.com/news/name-of-new
without making tons of php files and with specific description, keyword for each of them
The best answer here is to adopt a MVC programming method with one entry point that you define. Let me show a very basic example that will not involve using a pre-made framework. Basically you will have one entry point (usually the index.php file) and then for the most basic example one folder with modules for each page.
You have to program the index.php so that according to a parameter it will load the correct page file. So far this is just what you suggested with the ?id=27 thingy.
Now let's make it a little piper. Create a .htaccess file next to your index.php one. A .htaccess file can define rules for your whole website. Here you need to make some rewrite rules.
A rewrite rule is basically something that will tell your website to act like if the user asked index.php?id=27 whereas he asked yourdomain.com/news
So type first this code to enable the rewrite rule engine
RewriteEngine On
RewriteBase /
Then make a simple rewrite rule like this one
RewriteRule ^/(.*)$ /index.php?page=$1 [L]
Please keep in mind that you must use relative links otherwise this so far implicit redirection will show up in the browser nav bar.
There is a plenty of way to make rewrite rules, check Htaccess rewrite rule on the internet to find one that suits to your architecture.
EDIT: for preventing your rewrite rule to run when the requested url is a file (like an image) use these directives before the rewrite rule line
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Mod Rewrite with Pagination

I wanna make this rather simple to ask so I can hope for a simple response. I'm somewhat new to mod rewriting (most I've done is a small cms using index.php?page=x and mod rewriting to that name). I have a shopping cart created by foreign people for my company before I started working here with little to no documentation and they are asking me to make the cart search engine friendly. I won't get too dirty with the details, just need to ask a question.
I have, say, results.php?name=friendly-url. I've edited my .htaccess so I can access these pages with a friendly url. It works perfectly.
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1
Now the cart has it written to paginate kinda awkwardly only because the $_GET variable is stupidly named. I'm trying to find out, without having to get really dirty and having to re-name files or re-route directories in the code, to make a simple friendly pagination.
The end result I'm looking for is something like starter-kits-01.html and starter-kits-02.html and so on. This is the mod rewrite I've been trying just to get something to work.
RewriteRule ^[A-Za-z-0-9-]+).html?p=([0-9]+)$ results.php?name=$1&pageNum_rsCWResults=$2
That, I believed, should allow me to render starter-kits.html?p=2. I'm getting no mod rewrite error, but it's messing up my $_GET variables. I can't do, say, /starter-kits/2/ without getting dirty and having to go through this messy code the foreign people made and change 500 lines of directories.
I've spent about 30 minutes on it, and I have 3 other projects going on today, so I'm going to move onto those while I wait for somebody a little more experienced with mod rewriting to give me a helping hand.
Much appreciated.
Just use:
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1 [QSA]
The QSA part tells it to forward any GET parameters on to the rewrite.
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
The page rewriting you need to do should be done in PHP itself and only require a few lines in mod rewrite. I really recommend you download a copy of Drupal or WordPress to see how they do it. But basically here is how it should work.
You create a URL structure like this:
product-search/cat-toys
product-search/cat-toys/page-1 (should point to the same place as the previous URL)
product-search/cat-toys/page-2 (could also use "product-search/cat-toys/page/2)
You take your site and have everything relay through a central index.php file, mod rewrite takes care of this. You just use the URLs on your site and the PHP will take the params passed and parse it into the URL structure that then takes you to the results.
Essentially the URL path is passed to index.php to one parameter by mod rewrite.
Sample mod rewrite entry (from Drupal, very similar to Wordpress):
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Please check out these systems to get a better idea. WordPress is a much simpler, more straightforward system to go through.

.htaccess help to RewriteRule

I have a problem, surprise ;)
I use .htaccess in Apache and have a RewriteRule problem
My code is
RewriteRule ^(.*)$ /system/header_codes.php?oldurl=$1
How can I make if it did not have a true ( rewriterule ) so use this, I will use it becures i create a dyanmic RewriteRule for my customer in my System?
i hope for help here, sorry for bad spelling.
I might be way off here becuase I barely get what you're asking, but maybe this will help a little. It's the rewrite rule I use to send everything to my request handler (index.php). It doesn't redirect css, image, etc files, so maybe you can use that logic to exclude other patterns.
RewriteRule !(\.(css|jpg|png|gif|jpeg|js|swf))$ index.php [NC]
Your question is a little hard to understand, but if I'm not mistaken, you want to redirect the user to a custom page IF the rewrite rule is not matched.
Now I will explain the problem with that:
The rewrite rule is designed to intercept requests to certain URLs and redirect them to another page. If the rule is not matched, nothing happens and the request is handled normally by the webserver.
What you can do, is set a rule in your .htaccess file to redirect requests to nonexistent pages to another file. (See here)

Categories