Mod_rewrite question mark not appearing in output url - php

I'm trying to setup a simple mod_rewrite to redirect some open cart links which get broken when an import is done. Essentially the pretty urls for information pages don't go to where they should so im planning on overwriting them using .htaccess
Anyway I have a setup like so:
RewriteEngine On
RewriteBase /myshop.co.uk/
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L]
So as I understand the following
www.myshop.co.uk/about-us/why-shop
will display the page contents of
index.php?route=information/information&information_id=7
However when I test this its only returning
www.myshop.co.uk/index.php
Everything after and including the ? appears to be ignored, first I thought I had to escape it but after reading various posts it doesn't seem like I need to.
Also while I'm here, is it possible to specify a RewriteBase which includes sub directories? For example
RewriteBase /development/myshop/

Everything after and including the ? appears to be ignored,
No that's correct it is appending full URL as per your rule. Only thing is that since you're not using R flag this redirection is happening silently.
If you add R flag in your rule then you'll notice full URL being displayed in your browser:
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L,R]
Reference: Apache mod_rewrite Introduction

Related

How do i setup htaccess to allow changes to occur?

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.

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.

making existing url to seo friendly without making change to table

how to make this url to seo friendly urls
items.php?itemid=1&title=my title goes here
to
products/1/my-title-goes-here
how it can be done using only .htaccess not making any change to table or code currently data is access through id, don't even changing the query.
You have to use the Apache module mod_rewrite and the respective rewrite rules in the .htaccess file.
Read this it helped me a lot when I was first starting with it
Something like (not tested)
RewriteEngine on
RewriteBase /
RewriteRule ^products/([^/]+)/([^/]+)$ http://your web site/items.php?=$1&title=$2 [R=301,L]
which matches the patten that starts with products, with the first sub-patten being at least one character long, and anything that is not '/' . Same is true for the second pattern which must end the URL. You then rewrote that into the actual string your software needs.

Categories